javascript - 在屏幕上显示每个 div 的 jquery 效果

标签 javascript jquery html css

我正在制作一个单页网站,我希望每个 div 的操作仅在所述 div 在屏幕上时才起作用。

var fixed = false;
var topdivgrad = $('#cont2').offset().top - 200;
var topdivexp = $('#cont3').offset().top - 200;
$.fn.slideLeft = function () {
    return $(this).animate({
        "margin-left": "0px"
    }, 700); 
}
$.fn.rollUp = function () {
    return $(this).switchClass("hide-image", "show-image", 700, "easeInOutQuad"); 
}
$(document).ready(function () {
    $('#fade_object').animate({
        'margin-top': '8%' 
    }, {
        duration: 1000
    });
    $('#fade_object').animate({
        'margin-top': '3%'
    }, {
        duration: 200
    });
});
$(document).scroll(function () {
    if ($(this).scrollTop() >= topdivgrad && $(this).scrollTop() < topdivexp) {
        if (!fixed) {
            fixed = true;
            $(".title").slideLeft();
            $("div.image").rollUp();
        }
    }
    if ($(this).scrollTop() >= topdivexp) {
        if (!fixed) {
            fixed = true;
            $('#popup_object1').delay('500').animate({
                'width': '+=40',
                'height': '+=40', 
                'margin-left': '-=20',
                'margin-top': '-=20'
            }, 500, function () {
                $('#popup_object1').animate({
                    'width': '-=10',
                    'height': '-=10',
                    'margin-left': '+=5',
                    'margin-top': '+=5'
                }, 200, function () {
                    $('#year1').children("h1").animate({
                        'margin-top': '-=50',
                        'height': '+=50'
                    }, function () {
                        $('#year1').children("p").show();
                    });
                });
            });
            $('html, body').animate({ 
                scrollTop: topdivexp + 200
            }, 'slow');
        }
    }
});

不幸的是,这只适用于第二个 div;如果我在显示时刷新屏幕,它也适用于第三个 div。

最佳答案

既然您已经在使用 jQuery,我推荐这个非常小的插件:http://www.appelsiini.net/download/jquery.viewport.js

然后你可以使用这个选择器:$('div1:in-viewport')

关于javascript - 在屏幕上显示每个 div 的 jquery 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25127678/

相关文章:

javascript - 缩放和旋转后,HTML5 Canvas 会回移

javascript - Elasticsearch : How can i map json data having dynamic number of fields

javascript - jQuery urlencode/decode 补丁帮助

javascript - 如何绑定(bind)knockout viewmodel动态加载页面?

jquery - 悬停状态很古怪

jquery简单菜单翻转(无动画队列)

javascript - 如何在不替换数组内容的情况下在javascript中将数组内容添加(复制)到另一个数组?

html - 如何设置输入宽度只要里面的文字

Javascript:向 'Page Leave Confirmation' 添加条件?

javascript - 在 Vanilla JS 应用程序中是否仍然需要 $document.ready() 的现代等价物?