2013-03-23 32 views
6
Ben scroll olayı dinlemek için gereken bir proje üzerinde çalışıyorum

.. ben daha iyi bir yaklaşım ne olduğunu merak kaydırma ..jQuery setInterval veya

1 Yaklaşım

function scroll() { 
    if ($(window).scrollTop() > 200) { 
     top.fadeIn(); 
    } else { 
     top.fadeOut(); 
    } 
    if (menuVisible) { 
     quickHideMenu(); 
    } 
} 

2 Yaklaşım

 function scroll() { 
      didScroll = true; 
     } 

     setInterval(function() { 
      if (didScroll) { 
       didScroll = false; 
       if ($(window).scrollTop() > 200) { 
        top.fadeIn(); 
       } else { 
        top.fadeOut(); 
       } 
       if (menuVisible) { 
       quickHideMenu(); 
       } 
      } 
     }, 250); 

Teşekkür :)

+0

http benim için iyi çalıştı Pencere Kaydırma olay için bir örnek vardır: // ejohn. org/blog/learning-twitter/twitter John Resig tarafından yazılan bu makaleyi oku –

+0

@MohammadAdil Okudum ve bu soru hakkında düşünmeme sebep olan şey .. –

+2

@LuckySoni Ne yaptın? –

cevap

17

ne. JS/jQuery kalıplarını okuyordum. Buradan Aslen jQuery Window Scroll Pattern

var scrollTimeout; // global for any pending scrollTimeout 

$(window).scroll(function() { 
    if (scrollTimeout) { 
     // clear the timeout, if one is pending 
     clearTimeout(scrollTimeout); 
     scrollTimeout = null; 
    } 
    scrollTimeout = setTimeout(scrollHandler, 250); 
}); 

scrollHandler = function() { 
    // Check your page position 
    if ($(window).scrollTop() > 200) { 
     top.fadeIn(); 
    } else { 
     top.fadeOut(); 
    } 
    if (menuVisible) { 
     quickHideMenu(); 
    } 
}; 

: Javascript Patterns

+0

Bu Javascript Desenleri kütüphanesi altındır! – DawnPaladin

0

O

<style type="text/css"> 
    .scrollToTop { 
     width:100px; 
     height:130px; 
     padding:10px; 
     text-align:center; 
     background: whiteSmoke; 
     font-weight: bold; 
     color: #444; 
     text-decoration: none; 
     position:fixed; 
     top:75px; 
     right:40px; 
     display:none; 
     background: url('arrow_up.png') no-repeat 0px 20px; 
    } 

.scrollToTop:hover{ 
    text-decoration:none; 
} 

</style> 
<script> 
$(document).ready(function(){ 

    //Check to see if the window is top if not then display button 
    $(window).scroll(function(){ 
     if ($(this).scrollTop() > 100) { 
      $('.scrollToTop').fadeIn(); 
     } else { 
      $('.scrollToTop').fadeOut(); 
     } 
    }); 

    //Click event to scroll to top 
    $('.scrollToTop').click(function(){ 
     $('html, body').animate({scrollTop : 0},800); 
     return false; 
    }); 
}); 
</script> 

<a href="#" class="scrollToTop">Scroll To Top</a> 

http://www.paulund.co.uk/how-to-create-an-animated-scroll-to-top-with-jquery

+0

FadeIn() yönteminin öğeye önceden uygulanmış olup olmadığını kontrol etmek iyi mi? – KeizerBridge

İlgili konular