I am redesigning my portfolio website (finally after years of its being sorely out of date) and there is something I would like to do with my background image but I’m not sure if its possible with just css.
I am going to set it to completely fill the width of the viewport, but it is taller than the viewport. I would like it to scroll with the webpage until the bottom of the image hits the bottom of the viewport, and then stop scrolling (ie behave like the css background-attachment: fixed).
I’m thinking that I will have to use javascript here, so if I get no solution from the css gurus, I guess I will ask to have the thread moved to the javascript section. I have been concentrating on server-side scripting for the past four years or so, so my javascript is very rusty an d probably very out of date in terms of best practices.
looking fwd to seeing your portfolio site (need to do mine as well)
what about setting your image as a background image in an absolutely positioned div?
D
So to understand you better, lets say my viewport is 1000px and the image is 1500px. It will scroll down 500 more px and then at that mark, the image will fully have been shown. At that point, as I keep scrolling down, the image stays fixed in place?
If I were to scorll back up to the top 1000px of the viewport, it would unfix and scroll back up to the top of the image?
hmm i think i have an idea worth playing w/. & have started a code pen. I Don’t know about css @ronpat or @PaulOB or @ralphm would def know better.
But i think that with some simple jquery & using .offest you could do it.
use offset to say that when the page is at a certain offset from the top http://api.jquery.com/offset/
toggle a class that fixes it in place? as in position fixed?
D
The CSS wouldn’t be all that difficult. Think of how sticky menus are done. After a certain point, they take effect (they become fixed: the point this starts is based off of offset). That’s essentially what will need to be done here.
The CSS isn’t an issue - it will be pretty easy. It’s just getting the Javascript for it. If he makes a thread in the Javascript section they will be able to help him out fine.
not too overwhelm you but thought you might like to check out the code for this page. how they play w/the images might be a bit too much, but interesting
Wow, that’s really cool! And it’s done without any javascript. I definitely want to dig into that code to see how they did it. It might have a bit too much going on for my portfolio site, though.
Ehem, well clearly it was a typo on my part…whoops…
I had read on here about you saying you were a woman but it slipped my mind . I want to say you were a … teacher…? I have you bundled up with @James_Hibbard in terms of past lives…
nope sorry web. it’s got some js in there! or jquery but hopefully not too much.
din’t know you were a woman either. But i think ryan calls everyone “he” did it to me too
high five!
$( window ).scroll( function(){
var ypos = $( window ).scrollTop(), //pixels the site is scrolled down
visible = $( window ).height(), //visible pixels
img_height = 3171, //replace with height of your image
max_scroll = img_height - visible; //number of pixels of the image not visible at bottom
if ( max_scroll < ypos) {
$('body').css('background-attachment', "fixed");
$('body').css('background-position-y', "-" + (img_height - visible)+'px');
} else {
$('body').css('background-attachment', "scroll");
$('body').css('background-position-y', '0px');
}
});
I’m finding the background image repeats once the bottom of the image hits the bottom of the viewport. Or does it just jump back up to the top when it becomes fixed? (Hard to describe what I am seeing here). I would like it to become fixed at the bottom of the image.