简体   繁体   English

jQuery css()无法在条件语句中工作

[英]jQuery css() not in working in conditional statement

I am creating a simple parallax scroller that scales images to the screen width, sets a scrolling wrapper that is set to the scaled height of all the images, and looks at the scrollTop value to set top position of the images using jQuery's css(). 我正在创建一个简单的视差滚动器,将图像缩放到屏幕宽度,设置一个滚动包装器,该包装器设置为所有图像的缩放高度,并查看scrollTop值以使用jQuery的css()设置图像的顶部位置。 Here is the main function: 这是主要功能:

function positionSlides() {
    sT = $(window).scrollTop();
    $('div.slide img').each(function(){
        wp = $(this).attr('data-waypoint');
        if(sT >= wp){
            $(this).css({'top' : wp, 'border' : '1px solid red'});
            //console.log(sT + ':sT, ' + wp + ':wp');
        }
        else{
            $(this).css({'top' : ((wp-sT)/4)+sT, 'border' : '1px solid blue'});
        }
    });
}

The setting of the border colors is there to test which conditional statement is passing. 边界颜色的设置可以测试通过哪个条件语句。

positionSlides() is called on scroll via 通过滚动调用positionSlides()

$(window).scroll(function(){
    positionSlides();
});

My problem is when sT >= wp , the css property 'top' is not set to the value of wp . 我的问题是,当sT >= wp ,css属性“ top”未设置为wp的值。 It is set to what seems to be the last value passed by the else statement, which is some value slightly less than wp . 它设置为似乎是else语句传递的最后一个值,该值比wp略小。 But at the same time, 'border' : '1px solid red' is set correctly via the same if statement. 但同时,可以通过相同的if语句正确设置'border' : '1px solid red'

Why is the 'top' property not set in the if part of the statement? 为什么在语句的if部分中未设置'top'属性?

The first time you work with wp where it isn't working it is a string. 第一次在无法使用wp地方工作时,它是一个字符串。 The second time, it is an integer or float when you do wp-sT . 第二次,当您执行wp-sT时,它是整数或浮点数。 if you convert the first version to a numeric value it will work. 如果将第一个版本转换为数字值,它将起作用。

var wp = parseInt($(this).attr('data-waypoint'),10);

My guess would be that somehow wp isn't getting loaded with the expected value - not much room for anything else to go wrong here. 我的猜测是, wp不会以期望的值加载-这里没有太多空间可以出错。

Are you sure wp is pulling the value you expect? 您确定wp正在拉您期望的价值吗? How is the element's data-waypoint attribute being set - maybe that's the culprit? 如何设置元素的data-waypoint属性-也许是罪魁祸首?

If not, can you give us example values of what you expect top to be set to, and what it actually gets set to? 如果不是,您能否举例说明您期望的顶部设置和实际设置的值?

Your problem is that you're firing this on every single scroll event. 您的问题是您在每个滚动事件中都触发了该事件。 Which is a lot. 有很多 Check out this article on HTML5Rocks.com. 在HTML5Rocks.com上查看此文章 It basically suggests using requestAnimationFrame() and checking the position each time instead of doing it on a scroll event. 它基本上建议使用requestAnimationFrame()并每次检查位置,而不是在滚动事件上进行检查。

In your case, you could just change your code to 就您而言,您可以将代码更改为

requestAnimationFrame(positionSlides);

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

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