简体   繁体   English

淡入淡出/淡出div中的文本

[英]Fadein/Fadeout a text inside a div

I would like to know how could I make my div that contains text fade in from bottom to top when scrolling down the page? 我想知道当向下滚动页面时,如何使包含文本的div从下到上淡入? i will be grateful for your help. 我将感谢您的帮助。 Here is my Code: 这是我的代码:

 var $text = $('.textBlock'); $(window).on('scroll', function(event, element) { $text.each(function(event, element) { if ($(this).visible()) { $(this).children('p').stop().fadeIn(); } else { $(this).siblings('.textBlock p').stop().fadeOut(); } }); }); 
 .textBlock { text-align: center; width: 100%; height: 118px; float: left; display: block; } .textBlock p { font-size: 24px; padding: 30px 0; line-height: 25px; letter-spacing: 0.1em; display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="blockOne" class="textBlock"> <p>Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> <div id="blockTwo" class="textBlock"> <p>Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> <div id="blockThree" class="textBlock"> <p>Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> 

You need to use a timer function for this. 您需要为此使用计时器功能。 Check this out: 看一下这个:

 $(function () { $(".textBlock").hide(); $("#blockOne").show(); $(window).scroll(function () { numTextBlocks = $(".textBlock").length; $(".textBlock:visible").fadeOut(400, function () { console.log(".textBlock:nth-child(" + ($(window).scrollTop() * 10) % numTextBlocks + ")"); $(".textBlock:nth-child(" + ($(window).scrollTop() * 10) % numTextBlocks + ")").fadeIn(400); }); }); }); 
 html, body { height: 150%; } .textBlock { position: fixed; text-align: center; width: 100%; height: 118px; } .textBlock p { font-size: 24px; padding: 30px 0; line-height: 25px; letter-spacing: 0.1em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="blockOne" class="textBlock"> <p>One Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> <div id="blockTwo" class="textBlock"> <p>Two Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> <div id="blockThree" class="textBlock"> <p>Three Lorem ipsum dolor sit Amet, consectetuer adipiscing.</p> </div> 

This is what I used: 这是我使用的:

$(document).on("mousewheel", function () {
    $(".textBlock:not(:visible)").first().fadeIn("slow")
});

Here is the JSFiddle demo 这是JSFiddle演示

Let me know if this code works with you. 让我知道这段代码是否适合您。

Fiddle 小提琴

$(window).on("load",function() {
function fade() {
    $('.fade').each(function() {
        /* Check the location of each desired element */
        var objectBottom = $(this).offset().top + $(this).outerHeight();
        var windowBottom = $(window).scrollTop() + $(window).innerHeight();

        /* If the object is completely visible in the window, fade it in */
        if (objectBottom < windowBottom) { //object comes into view (scrolling down)
            if ($(this).css('opacity')==0) {$(this).fadeTo(500,1);}
        } else { //object goes out of view (scrolling up)
            if ($(this).css('opacity')==1) {$(this).fadeTo(500,0);}
        }
    });
}
fade(); //Fade in completely visible elements during page-load
$(window).scroll(function() {fade();}); //Fade in elements during scroll

}); });

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

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