Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/javascript: background-image, changes on scroll

I'll preface this by saying I am a beginner and my code is probably hillariously messy and/or incoherent, but I'll post it anyway hoping those errors can be caught.

I've searched for a couple days for answers and solutions to this, I also located this topic of someone asking the exact same question here (deemed to vague) so I'll be as thorough as I know how to be.

What I have so far, is a combination of a few tutorials and excerpts from here:


The concept: (A large amount of images will be pre-loaded and set as background images. In theory, for every whole number that is scrolled (scrollTop), the background image changes accordingly.)

All the images are basically named: imgname1.jpg, imgname2.jpg, imgname3.jpg etc. So the "imgname" is paired with the scrollTop number.

And here is what I have regarding only the scrolling (not the preload):

$(function getPos() {
    var Pos = $(window).scrollTop();  // "Pos = scrollTop and is added onto imgname
return Pos;
}); 

$(function() {

$(window).scroll(function() { //Defines that this function will be triggered as the scrollbar is moving 
    
    var image_url = 'http://www.examplehost.com/imgname' + Pos + '.jpg'
    
    if (getPos()>1) { //If the vertical position of scrollbar is above 1 display imgname + Pos + .jpg (ex: imgname30.jpg) and add to url

        $("body").css('background-image', image_url);                            
    }
    
    
    else { //If the vertical position of scrollbar is at 1 display start image
        $("body").css('background-image', url("http://www.examplehost.com/imgname/0.jpg");
    }

});

}); //function ends

I don't know how much of a mess that is and yes I'll be brushing up on the basics, since it is logical in my mind but completely wrong I'm sure. For this project however my knowledge only limits me to guessing/wondering the following:

  • Do I need a comma after my "}" when the functions end. ex: },
  • Is everything opened and closed properly.
  • This is exactly how my js file looks and I wouldn't be surprised if there was an issue of functions being called incorrectly.
  • Also instead of if (getPos()>1) I would like to use if (getPos()>=1)

Extra info that may help/further clarification, the goal is the image position remains still, and a new "frame" takes over with each scrolling increment. Ultimately, I would center + keep the images full screen.

Thank you for even reading this and I apologize if I broke a rule or if this question/dilemma has been solved in further detail somewhere I could not locate.

That said, thoughts?

like image 824
ScottFox Avatar asked Sep 12 '26 21:09

ScottFox


1 Answers

try changing this:

if (getPos()>1) { //If the vertical position of scrollbar is above 1 display imgname + Pos + .jpg (ex: imgname30.jpg) and add to url

    $("body").css('background-image', image_url);                            
}
else { //If the vertical position of scrollbar is at 1 display start image
    $("body").css('background-image', url("http://www.examplehost.com/imgname/0.jpg");
}

to:

if (getPos()>1) { //If the vertical position of scrollbar is above 1 display imgname + Pos + .jpg (ex: imgname30.jpg) and add to url

    $("body").css('background-image', 'url('+image_url+')');                            
}
else { //If the vertical position of scrollbar is at 1 display start image
    $("body").css('background-image', 'url(http://www.examplehost.com/imgname/0.jpg)');
}
like image 105
Dawson Loudon Avatar answered Sep 15 '26 10:09

Dawson Loudon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!