javascript - 滚动时如何使图像淡出可见

标签 javascript html css scroll

    <div id="smiley">
        <div id="star">
            <img src="inc/img/heks.png" class="star">
            <img src="inc/img/impo.png" class="star">
            <img src="inc/img/angst.png" class="star">
        </div>
    </div>

    #smiley{
        margin: 50px auto 80px;
        width: 1132px;
        height: 300px;
    }

    #smiley #star{
        display: none;
        float: left;
        height: 300px;
        width: 1132px;
    }

#smiley #star img.star{
    display: block;
    float: left;
    width: 300px;
    margin: 50px 38px 0px;
}

当我向下滚动到它们时,我需要让图像淡出可见。​​

我希望你能理解这个问题。

最佳答案

Demo .. Source code

如果你想只在图片出现在浏览器窗口时显示图片..而不影响它们的加载..

试试这个,让图像的visibility隐藏,然后当图像出现在浏览器..

所以 .. 在您的CSS 中:

<style>
    .star {
        visibility: hidden;
    }

    .fadeIn {
        -webkit-animation: animat_show 0.8s;
        animation: animat_show 0.8s;
        visibility: visible !important;
    }

    @-webkit-keyframes animat_show{
        0%{opacity:0}
        100%{opacity:1}
    }
</style>

然后加载jQuery

<script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

并在您的 JavaScript 中:

<script>
    function showImages(el) {
        var windowHeight = jQuery( window ).height();
        $(el).each(function(){
            var thisPos = $(this).offset().top;

            var topOfWindow = $(window).scrollTop();
            if (topOfWindow + windowHeight - 200 > thisPos ) {
                $(this).addClass("fadeIn");
            }
        });
    }

    // if the image in the window of browser when the page is loaded, show that image
    $(document).ready(function(){
            showImages('.star');
    });

    // if the image in the window of browser when scrolling the page, show that image
    $(window).scroll(function() {
            showImages('.star');
    });
</script>

希望这对你有帮助..

关于javascript - 滚动时如何使图像淡出可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747970/

相关文章:

javascript - 如何在点击(初始化)时设置一个 ng-click 按钮?

javascript - 如何在 JQuery Mobile 中保持页面刷新时选定的选项卡?

javascript - 使用 JQuery 更改选择/选项下拉列表中的现有值

jquery - 需要使用 twitter bootstrap 创建动态磁贴

html - 高级线性渐变动画 CSS,如 android swipetorefreshlayout

javascript - 如何不让网站阻止您的 iframe?

javascript - 我的 if 语句 "between"给出了错误的结果

javascript 在我的函数调用中使用 await

c# - ASP.NET Web 应用程序中的动态图像

css - 通过文本值在下拉菜单中选择一个值