简体   繁体   English

jQuery基于滚动位置的淡入淡出元素

[英]jQuery fade element based on scroll position

Based on this thread here: Fade in element on scroll down using css 基于此线程: 使用CSS向下滚动元素

I used the code in the accepted answer (with a slight change to how the animation runs) on a simple test page and it worked perfectly. 我在简单的测试页上使用了接受的答案中的代码(对动画的运行方式进行了少许更改),并且效果很好。 When I moved the code into a more complex page, it isn't working right. 当我将代码移到更复杂的页面时,它无法正常工作。 All I changed between my two pages was the selector for the element I want to animate. 我在两页之间进行的更改只是我要设置动画的元素的选择器。 After some digging, it appears it is getting the position of the desired element wrong. 经过一番挖掘后,看来它弄错了所需元素的位置。 What is it about the content on the page that would throw off the math on the position? 页面上的内容与该位置的数学运算无关吗? I can't link to my page, unfortunately, without some work, so if possible I'd like some possible suggestions before I do that. 不幸的是,没有一些工作,我无法链接到我的页面,因此,在可能的情况下,我想寻求一些建议。

Here is the JQ I am using: 这是我正在使用的JQ:

jQuery(document).ready(function(){

    $(window).scroll(function () {

        /* Check the location of each desired element */
        $('.fadein').each(function (i) {

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* If the object is completely visible in the window, fade it it */
            if (bottom_of_window > bottom_of_object) {

                $(this).delay(90 * i).animate({
                    'opacity': '1'
                }, 600);

            }
        });

    });

});

Thank you for any help! 感谢您的任何帮助!

The div was initially not visible.Check with div最初不可见。

jQuery('.fadein').css('opacity');

when the page loaded(without scrolling). 页面加载时(不滚动)。 The opacity was ititially 0 and it was way down in the page and so it came to visible when you started scrolling. 最初的不透明度为0,在页面中向下,因此在您开始滚动时就可以看到它。 Will try to fix that in a few minutes. 会在几分钟内尝试解决该问题。

Remove all Position=relative . 删除所有Position = relative It'll work. 会的

You're using jQuery in your .ready function, but $ inside. 您在.ready函数中使用jQuery,但在$中使用了jQuery。 Try this to create a namespaced alias: 尝试执行以下操作以创建命名空间别名:

jQuery(document).ready(function($) { ... });

or the simplified 或简化

jQuery(function($){ ... });

Lazy Load is delays loading of images in long web pages. 延迟加载是延迟在长网页中加载图像。 Images outside of viewport are not loaded until user scrolls to them. 除非用户滚动到视口之外的图像,否则不会加载它们。 This is opposite of image preloading. 这与图像预加载相反。

Using Lazy Load on long web pages will make the page load faster. 在长网页上使用延迟加载将使页面加载更快。 In some cases it can also help to reduce server load. 在某些情况下,它还可以帮助减少服务器负载。

If you want to load images according to scroll i think this is what you want! 如果您想根据滚动加载图像,我想这就是您想要的!

LazyLoad LazyLoad

Thanks for all the help, but I ended up replacing the above code with this: 感谢您提供的所有帮助,但最终我用以下代码替换了上面的代码:

http://imakewebthings.com/jquery-waypoints/ http://imakewebthings.com/jquery-waypoints/

and it's working fine. 而且工作正常。

Thanks again everyone. 再次感谢大家。

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

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