Parallax background image that fills container

送分小仙女□ 提交于 2019-12-11 17:27:22

问题


I'm using stellar.js to apply a parallax effect to a background image. The height of the container around the background image is set by javascript to the window height minus the height of the header. I have the background image set to cover in css and want it to fill the container regardless of the screen size. This works fine with no parallax effect, but once I apply the parallax effect the positioning changes, so when the container is tall the image doesn't fill it.

I spoke to Mark Dalgleish, the creator of stellar.js to see if he had any ideas, and he said that the background image has to be taller than the container for it to fill it, which is pretty much the conclusion I had come to. So I'm wondering if there is a different way I can accomplish this. Does anyone have any ideas?

Here's the site I'm trying to make it work on. If you resize the browser window by pulling it in from the side and reload the page you will see what I mean. There's a gap below the image. http://sonomarinwebdesign.com/agnitio


回答1:


I'm not quite sure if this would work but something to try is to add javascript that runs when the page resizes:

$(function() {
    $(window).resize(function() {
       // replace "100" with whatever makes it the best height:
       var newHeight = ($(window).height() - 100);
       var pictureHeight = 823;
       if (newHeight > pictureHeight) newHeight = pictureHeight;
       $(".an-yee-tsee-o.parallax.panel-pane").height(newHeight + "px");
    });
});

The point here is that you want to resize the parallax background panel when the window is resized. Even if this isn't quite right, hopefully it gets you on the right track. Best of luck!

Note - I was trying to fix the way the site looks when you load it on a "short" screen and then resize the browser larger. For your issue of the background not filling you can scale your background image up for that in a tool such as gimp for larger screens (or get a larger image to start with).

EDIT - Added code to make sure the div never ends up larger than the picture. You can also run this same code on load. It might not look as originally intended but it should work at least.




回答2:


I finally solved this by using skrollr.js instead of stellar.js, and then setting the background image to "background-attachment: fixed" in the css. The scripts give a similar effect, but scrollr.js starts with the top offset at 0 so it doesn't leave a gap below the image.



来源:https://stackoverflow.com/questions/22277060/parallax-background-image-that-fills-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!