简体   繁体   English

jQuery视差效果问题

[英]jQuery Parallax Effect Issue

I have a problem with jQuery parallax effect for my body's background. 我的身体背景存在jQuery视差效果问题。 Here is the code: 这是代码:

jQuery(document).ready(function($){
    window.onscroll = function() {
        var bh = $(document.body).height();
        var wh = $(window).height();
        var st = $(window).scrollTop();
        var p = wh / bh;
        var pp = (st * p);
        $('body').css({backgroundPosition: '50% -'+pp+'px'});
    }

});

This code is great but after I have added I header with logo and nav menu they closed some part of background so not looks good enought now. 这段代码很棒,但是在我添加带有徽标和导航菜单的标题后,它们关闭了背景的某些部分,因此现在看起来还不够好。 Here is the link also http://layot.prestatrend.com/ The height of my header is 129px by the way. 这里的链接也是http://layot.prestatrend.com/顺便说一下,我标题的高度是129px。 Seems I need to make background-position +129px but can't figured out how to make it work properly with javascript. 似乎我需要将背景位置设置为+ 129px,但无法弄清楚如何使其与javascript一起正常工作。 Any help please? 有什么帮助吗?

just add here: 只需在这里添加:

var topPosition = pp + 129;
$('body').css({backgroundPosition: '50% -'+topPosition +'px'});

(or) (要么)

 $('body').css({backgroundPosition: '50% -'+ (pp + 129) +'px'}); // the parentesis avoid concatenation also

this way you'll properly add the offset you want. 这样,您将正确添加所需的偏移量。 don't do it inside the string concatenation because the browser might append ==> wich would cause something like: for pp=99 ==> pp+129 = '99129' . 不要在字符串连接中执行此操作,因为浏览器可能会在==> wich pp+129 = '99129'添加==> wich,从而引起类似以下情况:for pp=99 ==> pp+129 = '99129'

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

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