2017-05-02 4 views
0

.fadeInエフェクトを.animateではなくスクリプトに追加しようとしましたが、動作しません。基本的には、スクロールして要素をフェードインさせようとします。これは、スクリプトやHTMLです:jqueryスクリプトで.fadeInエフェクトを追加するには?

$(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).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' 
 
     }, 1500); 
 

 
     } 
 
    }); 
 
    }); 
 
});
.hideme { 
 
    opacity: 0; 
 
    height: 300px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div>

+0

そして、あなたが作業コードではなく、非動作するコードをポストすることを決めましたか? – adeneo

+0

あなたの要素は見えるので、不透明度はなく、 'fadeIn'を使いたくない場合は、実際に' animate'やjQueryの 'fadeTo'を使いたいと思っています。 – adeneo

+0

そしてfadeInを追加する方法。一例を示してください。私はfadeInがアニメーションよりも見栄えが良いと思うので心配しています、そうですか? –

答えて

0

$(document).ready(function() { 
 
\t /* Every time the window is scrolled ... */ 
 
\t $(window).scroll(function() { 
 
\t \t /* Check the location of each desired element */ 
 
\t \t $('.hideme').each(function(i) { 
 
\t \t \t var bottom_of_object = $(this).offset().top + $(this).outerHeight(); 
 
\t \t \t var bottom_of_window = $(window).scrollTop() + $(window).height(); 
 
\t \t \t /* If the object is completely visible in the window, fade it it */ 
 
\t \t \t if (bottom_of_window > bottom_of_object) { 
 
\t \t \t \t $(this).fadeIn(400, function(){ 
 
\t \t \t \t \t $(this).css('opacity', 1) 
 
\t \t \t \t }); 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
});
.hideme { 
 
    opacity: 0; 
 
    height: 300px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div> 
 
<div class="hideme"> 
 
    <p>This is some text.</p> 
 
</div>

+0

このコードスニペットは質問を解決するかもしれませんが、[説明を含む](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)は本当にあなたの投稿の質を向上させるのに役立ちます。将来読者の質問に答えていることを覚えておいてください。そうした人々はあなたのコード提案の理由を知らないかもしれません。 –

+0

確かにパトリック、提案をありがとう –

関連する問題