简体   繁体   English

jQuery fadeTo()函数与scrollTop()

[英]JQuery fadeTo() Function with scrollTop()

I try to use fadeTo() in a scrollTop() function. 我尝试在scrollTop()函数中使用fadeTo()。 First, I try it with fadeIn and fadeOut, that works perfect. 首先,我尝试使用fadeIn和fadeOut进行测试,效果很好。 But I want to keep the true height of the image. 但是我想保持图像的真实高度。 I changed the opacity in my css to 0 and want to show the image, when the scrollposition is between 30 and 200. 我将css中的不透明度更改为0,并希望在滚动位置在30到200之间时显示图像。

    <script type="text/javascript">
        $(document).scroll(function () {
            if ($(this).scrollTop() > 30 && $(this).scrollTop() < 200) {
                $("#amy").fadeTo("slow", 1);
            } else {
                $("#amy").fadeTo("slow", 0);
            }
        });
    </script>

CSS: CSS:

#amy {
    padding-top: 20px;
    margin-bottom: -50px;
    max-width: 155px;

    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    -moz-opacity: 0;
    -khtml-opacity: 0;
    opacity: 0;
}

The if is working with fadeIn $ fadeOut, but not with fadeTo. if与fadeIn $ fadeOut一起使用,但与fadeTo不一起使用。 Dont know why? 不知道为什么? Is fadeTo not supported with scrollTop? scrollTop不支持fadeTo吗?

I'm not sure why fadeTo isn't working, but you can try animate() 我不确定为什么fadeTo无法正常工作,但是您可以尝试animate()

<script type="text/javascript">
    $(document).scroll(function () {
        if ($(this).scrollTop() > 30 && $(this).scrollTop() < 200) {
            $("#amy").animate({opacity: 1}, "slow");
            return;
        } 
        $("#amy").animate({opacity: 0}, "slow");

    });
</script>

Also, make sure there is no other CSS on the element. 另外,请确保该元素上没有其他CSS。 Like visibility: hidden; visibility: hidden;一样visibility: hidden; or anything like that. 或类似的东西。

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

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