SORU
2 NİSAN 2012, PAZARTESİ


Etkin Menü Sayfa Kaydırma Öğesini değiştirmek?

Burada görülecek ki bu özellik iyi bir örnek: http://www.maddim.com/demos/spark-r6/

Sayfayı aşağı doğru kaydırırken, aktif menü öğesi değiştirir. Bu nasıl yapılıyor?

CEVAP
2 NİSAN 2012, PAZARTESİ


Kap binding to the scroll event (genellikle pencere) bitti.

Hızlı bir örnek:

// Cache selectors
var topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight() 15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop() topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";
   // Set/remove active class
   menuItems
     .parent().removeClass("active")
     .end().filter("[href=#" id "]").parent().addClass("active");
});​

Yukarıda in action at jsFiddle kaydırma animasyon dahil, bkz.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • njhaley

    njhaley

    24 NİSAN 2006
  • Blu animations and other videos

    Blu animatio

    15 HAZİRAN 2007
  • Truc Minh

    Truc Minh

    23 Ocak 2011