简体   繁体   English

从现有的50%背景位置y开始的jquery视差效果

[英]jquery parallax effect starting from an existing 50% background-position-y

I've been working on some parallaxing using a few different parallax plugins as well as some home built parallax code. 我一直在使用一些不同的视差插件以及一些自制的视差代码进行一些视差工作。 My problems is that all the parallaxing code I've looked at adjusts the background-position-y by pixels. 我的问题是我看过的所有视差代码都按像素调整背景位置y。 I'm trying to start off with my background image set to cover and an initial background-position as center center or "50% 50%". 我试图将背景图像设置为覆盖,并以初始背景位置为中心或“ 50%50%”作为开始。 When the parallax code kicks in, I believe it's assuming it's starting from a background-position-y of 0px. 当视差代码启动时,我相信它是假设它从0px的background-position-y开始。

I would like to start my parallax from the current position of the background-image. 我想从背景图像的当前位置开始我的视差。 This is difficult when that position is 50%. 当该位置为50%时,这是困难的。 Any ideas how I might be able to do this? 有什么想法我可能能够做到这一点吗? Here's some example code. 这是一些示例代码。 Hope it makes sense. 希望有道理。

using Ian Lunn's plugin: http://www.ianlunn.co.uk/plugins/jquery-parallax/ 使用Ian Lunn的插件: http ://www.ianlunn.co.uk/plugins/jquery-parallax/

    section {
        position: relative;
        width: 1900px;
        height: 834px;
        overflow: hidden;
        background: 50% 50% no-repeat fixed;
        background-size: cover;
        background-image: url("img/image_1.jpg");
     }

jquery: jQuery的:

$('section').parallax("50%", .6);

The plugin makes some calculations based on the container height and scroll position and uses the speed to calculate a new y position, only it doesn't take into account the existing y position (first of all), but that could easily be added in there. 该插件根据容器的高度和滚动位置进行一些计算,并使用速度来计算新的y位置,只是它不考虑现有的y位置(首先),但是可以很容易地在其中添加。 I just don't know how to do it if there is no exact Y position. 如果没有确切的Y位置,我就是不知道该怎么做。 The result is that there's a jump when you start scrolling as the original background-position-y of 50% is overridden with, say -10px. 结果是,当您开始滚动时会发生跳跃,因为50%的原始背景位置y被-10px覆盖。

make sense? 说得通? Unfortunately, I don't have a working example to show. 不幸的是,我没有一个可行的例子来展示。 thanks 谢谢

I amended calculation of var which controls background position by adding the difference (instead of top:0 top:50% ). 我修改了通过添加差异(而不是top:0 top:50% )来控制背景位置的var的计算。 First you need to get the image height : 首先,您需要获取图像高度

var path = $('#id').css('background-image').replace('url', '').replace('(', '').replace(')', ''); 

var tempImg = '<img id='tempImg' src='+path+' />';

$('html').append(tempImg);
$('#tempImg').hide(); //hide image
var height = $('#tempImg').height(); //get height
$('#tempImg').remove(); //remove from DOM

Then get the container height and the position of 50% in px: 然后获得容器的高度和以像素为单位的50%的位置:

var contheight = $('#id').height();
var start = (-(height - contheight) / 2);

Then add this to the stating position. 然后将其添加到说明位置。 Mine was: 我的是:

var yPos = start + ( $(window).scrollTop()- $('#id').offset().top) * (1-ratio); 

Or use some other more flexible parallax script . 或者使用其他更灵活的视差脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM