Ways to modify the jquery script to display on page load rather than on scrolling action

Currently in the process of working on my site, www.runebs.dk.

Utilizing a script to activate information for each project.

JS..

$('.showHide').on('click',function(){
    $(this).toggleClass('active');
    $( '#subHeadline .description' ).slideToggle(200);
    var currentState = $(this).text();
    console.log('state: ', currentState);
    if($(this).hasClass('active')) {
        $('.showHide').empty();
        $(this).text('(+)');
    } else {
        $(this).text('(–)');
    }
});
}

$(document).ready(function(){
    // initializing toggleInfo when document is ready
    toggleInfo();
    var $window = $(window);
    $window.trigger('scroll'); // setting initial value
    $window.on('scroll', function(){

        var pos = $('#subHeadline').offset();
        $('.article-header').each(function(){
            if (pos.top <= $(this).offset().top && pos.top >= $(this).next().offset().top) {
                var desc = $('.description', this).text();
                var title = $('.h3', this).text();
                var year = $('.year', this).text();
                var button = $('.showHide', this).text();
                $('#subHeadline .title').text(title);
                $('#subHeadline .year').text(year);
                $('#subHeadline .description').text(desc);
                $('#subHeadline .showHide').show();
                return;
            }
        });
    });

CSS..

.article-header {
    display: none;
}
#subHeadline {
    display:none;
    position:fixed;
    top:0px;
    left: 0;
    margin-left: 22px;
    padding-top: 64px;
    height: auto;
    width: 600px;
    z-index:1;
}
#subHeadline .description {
    display: none;
    width: 280px;
    position: relative;
    margin-top: 28px;
}
#subHeadline .showHide {
    display: none;
    width: 280px;
    position: relative;
    margin-top: 28px;
    cursor: pointer;
}
.year {
    display: block;
    margin-top: -19px;
}

The current script functions perfectly, however I would like to adjust it so that the information for the first project is displayed upon entering the site instead of requiring scrolling.

If anyone could offer assistance, it would be greatly appreciated. Thank you..

Answer №1

Check out the code snippet below:

$window.ready(function() {
      // Insert your code here
    });

Answer №2

Here is a possible solution for what you are looking for

View the demo on Full Screen

Js fiddle

Jquery

    $(window).load(function(){

          var target = $("#section3") // this should be the target where to scroll
          $target = $(target);

          $('html, body').stop().animate({
            'scrollTop': $target.offset().top
            }, 1000);


    })

UPDATE

In response to your feedback, I have made some modifications to your JavaScript code and it should now function correctly

  • When the page loads, the script checks the first child element and displays its title and description
  • When scrolling, if the scroll position reaches another post, it will display that post's title and description.

    $(document).ready(function() {
    
    // executing toggleInfo when document is ready
    toggleInfo();
    
    var value = $(".article-header:first")
    
    var desc = $('.description', value).text();
    var title = $('.h3', value).text();
    var year = $('.year', value).text();
    var button = $('.showHide', value).text();
    $('#subHeadline .title').text(title);
    $('#subHeadline .year').text(year);
    $('#subHeadline .description').text(desc);
    $('#subHeadline .showHide').show();
    return;
    });
    
    var $window = $(window);
    
    $window.trigger('scroll'); // initializing the value
    
    $window.on('scroll', function() {
    
    
    var pos = $('#subHeadline').offset();
    
    $('.article-header').each(function() {
        if (pos.top <= $(this).offset().top && pos.top >= $(this).next().offset().top) {
            var desc = $('.description', this).text();
            var title = $('.h3', this).text();
            var year = $('.year', this).text();
            var button = $('.showHide', this).text();
            $('#subHeadline .title').text(title);
            $('#subHeadline .year').text(year);
            $('#subHeadline .description').text(desc);
            $('#subHeadline .showHide').show();
            return;
        }
    
    });
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disabling and enabling a link before and after an AJAX call using jQuery

I have been trying to disable a link before making an ajax call and then re-enable it right after receiving the response. However, my current approach doesn't seem to be working as expected: jQuery(this).prop('disabled', false); Here is my ...

Using Django and Jquery to pass multiple values to HTML, with one value stored in a .js file

Here is an example of HTML code: {% for m in mortgages %} <input id ='mortgages_counter' name='mortgages_counter' type='hidden' value='{{ m.MonthlyPaid }}'> {% endfor %} If used for a selection menu: {% for ...

Can someone please help me figure out why the "setInterval" function in my script isn't functioning as expected?

I've been experimenting with controlling the refresh rate of drawn objects in a canvas using JavaScript. Even after going through the tutorials and examples on w3.school, I'm still unsure why the "setInterval" function is not executing the "gener ...

What is the best way to align the bottom of an element with the browser buttons panel on mobile devices?

When viewing the website on a mobile device, I noticed that the floating button is hidden behind the browser buttons panel. Is there a way to adjust the positioning of the element relative to the browser's buttons panel? I attempted using env(safe-ar ...

Tips to position a div directly on top of table cells using absolute positioning

I have a div inside the second table cell that I want to position absolutely right outside the <td>. Currently, the issue is that the <td> elements below are overlapping the div. Eventually, each <td> will contain a div that appears on ro ...

Prevent border duplication in CSS and retain border visibility

Check out this fiddle where I have three divs with a border-width of 2px. Originally, the issue was that each div's border gets duplicated in between, resulting in a total border-width of 4px. To solve this, I applied a margin-top of -2px to each div, ...

Exploring how CSS affects backgrounds in the Google Chrome browser

Having an issue with the background on my website. In Firefox and other browsers, the background appears much lighter and whiter compared to Chrome. This is the CSS code for my background: body {background:#ffffff url(../../images/background.jpg); direct ...

Guidelines for attaching a div to the bottom of a page?

I have a navigation menu inside div.wrapper, and I am trying to make the div.wrapper stick to the footer. <div class="wrapper"> <div id="menu"> <ul> <li>1.</li> <li>2.</li> ...

CodePen displaying CSS errorsI'm having trouble getting my CSS

I'm facing an issue with my CSS code and I need your help to figure out what's wrong. I believe it's a simple problem that someone can quickly identify just by looking at the code. Any assistance would be greatly appreciated! Here is the co ...

How can I ensure that my image remains responsive and aspect ratio-friendly?

Is there a way to accomplish this magical effect? I find myself in a similar situation. I require an image that remains centered while adjusting to fit the screen, all while maintaining its aspect ratio. ...

Flexible DIVs with a maximum width of 50% that adjust to different

I'm having trouble getting these two DIVs to display properly within a parent DIV while maintaining responsiveness. I want them to each take up 50% of the screen with a 10px margin between them. Does my current code approach seem correct? .box-cont ...

Every symbol located at the top of the <td> element

SOLVED by P. Leger: In my CSS I just modified the vertical-align property to top, which successfully resolved the issue for me I am working on creating a basic schedule system where the admin has the ability to manage users and assign them to specific dat ...

Is it possible to manipulate the content inside a div tag using JavaScript in HTML?

What is the most efficient way to dynamically adjust a data offset value within an HTML tag using JavaScript? For example: <div class="box" data-offset="2s"></div> I am looking to update the value within the data-offset attribute based on s ...

Clicking will cause my background content to blur

Is there a way to implement a menu button that, when clicked, reveals a menu with blurred content in the background? And when clicked again, the content returns to normal? Here's the current HTML structure: <div class="menu"> <div class=" ...

Is there a way for me to manage the appearance of the printed document's layout?

I have successfully added 3 print buttons to my website, each one designed to print a specific portion (div) of the webpage. Here is the code snippet for this functionality: function printPage(id) { var html="<html>"; //html+="<link rel="st ...

Applying CSS transformations to scale up a div within its parent body

Having an issue with formatting an anchor link using transform scale attributes on hover. The link is inside a div that's aligned to the right, and I want it to enlarge when hovered over. However, this increase in size also affects the parent div, ca ...

What is the correct way to maintain focus on a nested scrolling div over another scrolling div with jQuery?

I have encountered an issue with a modal view that contains content extending beyond its boundaries. To remedy this, I applied the overflow-y: scroll property. However, the problem arises when the modal view overlaps with the body, which also has scrolling ...

Utilizing AJAX to send a parameter to PHP for processing

I am facing an issue with a script that is supposed to send data to a PHP file when a user clicks on an element, but unfortunately, it's not functioning correctly. Below is the jQuery code: jQuery( document ).ready(function( $ ) { $('.rve_b ...

What steps should I take to make my Jquery slider functional?

I could really use some assistance to make this work. I've attempted a few JQuery slider tutorials, but it seems like I'm missing something important? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <t ...

Laravel method to send back JSON error messages through the use of an "error" object in the response

My aim is to utilize the "Bootstrap File-input" plugin for JQuery to facilitate file uploads via AJAX. This plugin expects error messages to be received in a JSON key labeled "error" as shown below: {error: 'You are not allowed to upload such a file. ...