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

Breaking Points in Tailwind are crucial for creating responsive designs

I am attempting to create a simple div where I want px-5 for mobile screens and px-20 for larger screens. However, only px-5 is working and px-20 is not applying on large screens. Can someone help me understand why? Code Output Image <div class=" ...

Changing Identification and Classification into a String

I found this code snippet on a website dedicated to product sales. <html> <body> <div class="descr"> <div id="productName"></div> </div> </body> </html> The #produc ...

Guide to displaying highcharts using external json data for numerous series

Having an issue with using angularjs and highcharts with multiple series where the data is coming from an external JSON file. When trying to access the data with a for loop using data.data[i].data, it's not working properly. Can anyone offer assistanc ...

Using the URL parameter in a jQuery AJAX request

When I try to make an ajax call from a modal window, the request URL doesn't seem to be correct. My webserver is set up to accept URLs like http://localhost/search, but not file:///search. How can I fix this issue? I have posted more details about wh ...

Is there a way to customize the appearance of an unordered list by setting it to display as an image instead of default bullets? I want to

I have been attempting to achieve this desired outcome. However, my efforts to reproduce it resulted in the check marks being rendered at a smaller size than intended due to using an SVG file. An example of this issue can be seen in the following image: I ...

Enhanced Interactivity: jQuery UI Dialog Button Transforms Position upon Mouse Hover

I am utilizing the jQuery UI dialog feature and implementing buttons using jQuery in the following manner: $("#151").dialog({ autoOpen: false, autoResize: true, buttons: { "OK": function ...

How to toggle the dropdownbox status using jQuery

Just starting with jQuery and I'm looking to enable or disable a dropdown list based on a checkbox. Here's what my html looks like: <select id="dropdown" style="width:200px"> <option value="feedback" name="aft_qst">After Quest&l ...

The extent of a nameless function when used as a parameter

I am currently developing a straightforward application. Whenever a user hovers over an item in the list (li), the text color changes to green, and reverts back to black when the mouse moves away. Is it possible to replace lis[i] with this keyword in the ...

What is the best way to ensure Font-Face loads fonts successfully on iOS devices?

In the process of constructing a website with Gatsby.js, Styled-Components, and a custom font named 'Montserrat', I encountered an issue regarding font loading. The font appears as expected on desktop browsers during both build and hot-reloading, ...

Discover the color value within an array that begins with the "#" symbol

In my PHP code, I have written a function that extracts values from a CSS file and creates an array. Now, I need to loop through this array and create another array that only contains color values (strings starting with #). The challenge is that the length ...

Swap out the HTML tags with JavaScript code

I've been looking everywhere, but I couldn't find the answer. Here is my issue / question: I have a page from CKEDITOR with: var oldText = CKEDITOR.instances.message.getData(); So far, so good. The string looks something like this: <table ...

How can I customize the color of the title and property value in Handlebars?

Here is the list I am working with: <li>Equipment Area:{{equipment_area}}</li> <li>Equipment Type: {{equipment_type}}</li> <li>Manufacturer: {{manufacturer}}</li> <li>Model Number: {{model_number}}</li> < ...

Using Laravel, utilize Ajax and jQuery to populate a table by passing an array

Recently, I implemented a search bar specifically designed to locate someone's policy. Upon entering their policy number, the search provides details such as claims and policy wording. Initially, this process involved a redirection back to the same pa ...

Is there a way to modify the CSS or add custom styling within an iframe form?

Currently I am working on the following page: , where an embedded javascript form called infusionsoft (iframe form) needs to be made responsive at the request of my client. I'm wondering if there is a way to override the css or inject custom styles i ...

The Reason Behind Angular Error when Using CSS Custom Tags

I have the following SCSS code: main{ width: 100%; height: 840px; /*background: red;*/ margin: 10px auto; position: relative; padding: 5px 0; } section{ width: 98%; height: 600px; margin: auto; display: flex; ...

The functionality of triggers is not applicable to content that is dynamically loaded through ajax requests

I encountered a problem recently that required me to rewrite some functions. The original code looked like this: $('#type_of').on('change', function () { However, due to issues with manipulating the DOM and loading content via ajax, I ...

Is it feasible to utilize jQuery without relying on IDs for selection?

I am currently troubleshooting a code snippet in which jQuery seems to be accessing an element by ID even though the element does not actually have an ID assigned. Here is what I have in the markup: @Html.CheckBoxFor(Model => Model.IsEBSToBeCreated, new ...

Tips on aligning images of various sizes with text within a row using CSS

Looking for help aligning four different height (ranging from 160-180px) svg images with text positioned directly below each image. I want the images to be neatly aligned in a row, but I'm struggling to align the short text underneath them in a single ...

Transforming a jQuery Serialized URL into a JSON Object

I have come across various examples, but none seem to solve my specific issue. In the past, I serialized a form with jQuery for AJAX processing. However, now I am looking to convert that same serialized form into a JSON object. The existing examples did no ...

Retrieving data from a WebGrid on the front end

Utilizing a WebGrid to display information in the following manner: <div id="interiorGrid"> @{ var interiorList = Model.Lists; var interiorGrid = new WebGrid(interiorList, columnNames: new ...