Jquery 使用css向下滚动时的淡入元素
示例: 向下滚动时,每篇文章在浏览器的查看端口中显示。css是Jquery 使用css向下滚动时的淡入元素,jquery,css,Jquery,Css,示例: 向下滚动时,每篇文章在浏览器的查看端口中显示。css是 #examples article { -webkit-transition: opacity 200ms ease-in-out; -ms-transition: opacity 200ms ease-in-out; -moz-transition: opacity 200ms ease-in-out; -o-transition: opacity 200ms ease-in-out; tr
#examples article {
-webkit-transition: opacity 200ms ease-in-out;
-ms-transition: opacity 200ms ease-in-out;
-moz-transition: opacity 200ms ease-in-out;
-o-transition: opacity 200ms ease-in-out;
transition: opacity 200ms ease-in-out;
position: relative;
margin-top: 20px;
clear: both;
}
我尝试了这个css,但它不工作。是否有一些javascript与css一起工作?如何实现这种滚动->淡入淡出效果?
你想要这样的吗
Mohammed的回答不考虑任何绝对定位的图像。。。我们应该使用偏移量而不是位置
$(window).scroll(function () {
/* Check the location of each desired element */
$('.article').each(function (i) {
var bottom_of_object = $(this).offset().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).animate({
'opacity': '1'
}, 500);
}
});
});
最佳流程:
HTML:
<div id="container">
<div id="monster">Hello</div>
<div>Hello</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
</div>
$(function(){ // $(document).ready shorthand
$('.monster').fadeIn('slow');
});
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').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).animate({'opacity':'1'},1500);
}
});
});
});
#container
{
height:2000px;
}
#container DIV
{
margin:50px;
padding:50px;
background-color:pink;
}
.hideme
{
opacity:0;
}
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;
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).animate({'opacity':'1'},500);
}
});
});
});
CSS:
<div id="container">
<div id="monster">Hello</div>
<div>Hello</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
</div>
$(function(){ // $(document).ready shorthand
$('.monster').fadeIn('slow');
});
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').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).animate({'opacity':'1'},1500);
}
});
});
});
#container
{
height:2000px;
}
#container DIV
{
margin:50px;
padding:50px;
background-color:pink;
}
.hideme
{
opacity:0;
}
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;
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).animate({'opacity':'1'},500);
}
});
});
});
因为我在评论中读到了: 如果要在对象的1/3可见时使对象淡入,请使用以下代码: jQuery:
<div id="container">
<div id="monster">Hello</div>
<div>Hello</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
</div>
$(function(){ // $(document).ready shorthand
$('.monster').fadeIn('slow');
});
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').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).animate({'opacity':'1'},1500);
}
});
});
});
#container
{
height:2000px;
}
#container DIV
{
margin:50px;
padding:50px;
background-color:pink;
}
.hideme
{
opacity:0;
}
$(document).ready(function() {
/* Every time the window is scrolled ... */
$(window).scroll( function(){
/* Check the location of each desired element */
$('.hideme').each( function(i){
var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;
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).animate({'opacity':'1'},500);
}
});
});
});
我唯一更改的行是:
var bottom_of_object = $(this).position().top + ($(this).outerHeight())/3;
通过将
/3
更改为例如/4
,当对象的1/4可见时,您可以让它淡入淡出。尝试使用。为什么定义“i”,但从不使用它?为什么伙计们,如果我在某一时刻上下滚动,效果会崩溃?嗨@mohammad,这个脚本的thanx。它的工作原理与预期一样,但我只注意到了一个缺点,即div只有在您滚动了视口的3/4后才会显示。有没有办法使它显示在视口的四分之一处?只有在我将此脚本的.position()更改为.offset()Hi@K.H.thanx时,这才对我有效。它的工作原理与预期一样,但我只注意到了一个缺点,即div只有在您滚动了视口的3/4后才会显示。有没有办法使其显示在视口的四分之一处?