0

I have some elements on my website that need to be updated every time page is being scrolled or resized. So I made a small jQuery code to do that.

$(window).scroll(function(){
    setSlideshowButtons();
});

$(window).resize(function() { 
    setSlideshowButtons();
});

function setSlideshowButtons() {
    var size = $(".slides img").height(); 
    $(".navigation_buttons").css("margin-top", "-" + size/2 + "px");
}

And so far everything worked great. But than I found out that I also have to set those properties on document load. So I also put this in the document.ready function.

$(document).ready(function(){
    setSlideshowButtons();
});

But it doesn't seem to work. But when I scroll or resize the window, function runs itself and CSS properties change.

3 Answers 3

1

If you are doing any manipulations like creating a Slide Show and manipulating them, then you need to execute the setSlideshowButtons() function after the plugin's initialization has been called.

Say, example:

$(document).ready(function () {
    $(".slider").slider({
      option: value,
      option: value
    });
    // Place it after the init.
    setSlideshowButtons();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that was it. Thanks a lot.
0

Looks like image still doesn't load, on document ready. Try to re-call function on image load.

var img = $(".slides img");
    img.onload = setSlideshowButtons;

1 Comment

That's not the issue.
0
$(document).ready(function(){
    var size;
    var setSlideshowButtons = function () {
      size = $(".slides img").height(); 
      $(".navigation_buttons").css("margin-top", "-" + size/2 + "px");
    }
    setSlideshowButtons();
    $(window).on('resize scroll', setSlideshowButtons);
});

1 Comment

That's not the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.