【问题标题】:FadeOut image at the top as it moves up with animate顶部的淡出图像随着动画向上移动
【发布时间】:2018-03-02 18:10:16
【问题描述】:

我有一个使用 jquery animate 向上移动的绝对定位图像。它被一个 div 隐藏在顶部和底部(效果是观众看到一个图像在手机屏幕上向上滚动)。

我现在希望图像在到达顶部框架 div 时淡出。我认为它需要在到达“隐藏”div 的顶部之前淡出。我不知道如何只让图像的顶部在到达这一点时褪色。

HTML

 <div class="comments-hider">
     <img id="comments-feed" src="images/mobilestory-comments.png" alt="">
 </div>

CSS:

.comments-hider-image{
    position: absolute;
    top: 0;
    left: 15px;
    height: 696px;
    overflow-y: hidden;
    z-index: 700;
    width: 348px;
}

#comments-feed{
    position: absolute;
    top: -9px;
    left: 30px;
    width: 624px;
    z-index: 600;
}

jquery:

$("#comments-feed").animate({'top': '-1500px'}, 12000);

我的尝试:

var topOfOthDiv = $("#comments-feed").offset().top;
var nearTheTop  = $(".comments-hider").position({ top: 100, left: -200 });
if(topOfOthDiv > nearTheTop ) { //scrolled past the other div?
       $("#comments-feed").fadeOut(100);
        }

【问题讨论】:

    标签: jquery jquery-animate fadeout


    【解决方案1】:

    我假设您正在寻找类似的东西:

    progress: function (animation, progress, remainingMs) {
        var h = $(this).position().top + $(this).height();
        if(h <= 0) {
            $(this).stop().animate({top: '-9px'});
        }
    }
    

    $("#comments-feed").animate({
            top: '-1500px'
        },
        {
            duration: 12000,
            progress: function (animation, progress, remainingMs) {
                var h = $(this).position().top + $(this).height();
                if (h <= 0) { //go back
                    $(this).stop().animate({top: '-9px'});
                }
            }
        });
    .comments-hider-image {
        position: absolute;
        top: 0;
        left: 15px;
        height: 696px;
        overflow-y: hidden;
        z-index: 700;
        width: 348px;
    }
    
    #comments-feed {
        position: absolute;
        top: -9px;
        left: 30px;
        width: 624px;
        z-index: 600;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <div class="comments-hider">
        <img id="comments-feed" src="https://dummyimage.com/200x200/000/fff&text=1111" alt="">
    </div>

    【讨论】:

    • 谢谢你,但我想我被误解了。我需要图像向上移动,就像你一样,但是当它到达它所在的 div 的顶部时淡出(在顶部)。图像很长。 div 很短。这有意义吗?
    猜你喜欢
    • 2021-04-13
    • 2016-02-07
    • 2023-04-10
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多