2013-06-01 29 views
1

以下是我已经尝试,甚至我都试过添加新的DOM元素waypoints不工作在新元素只工作在旧的元素?

$(document).ready(function() { 
    $('.entry-item').waypoint(function (direction) { 
     if (direction === "down") { 
      $(this).children(".sticky-items").css({ 
       "position": "fixed", 
       "top": "45px", 
       "right": "0px" 
      }); 
     } else if (direction === "up") { 
      $(this).children(".sticky-items").removeAttr("style"); 
     } 
    }, { 
     offset: 5 
    }); 
    window.addmore = function() { 
     var $newdiv1 = $('<div class="entry-item"><div class="sticky-items">STICK XX</div><div>'); 
    $('#entries').append($newdiv1); 
     $.waypoints('refresh'); 
} 
}); 

$ .waypoints(“刷新”)所必需 帮助不工作!

http://jsfiddle.net/VxqNa/9/

回答

2

应重新初始化动态地添加的元素航点太:

DEMO

var initwaypoint = function (direction) { 
     if (direction === "down") { 
      $(this).children(".sticky-items").css({ 
       "position": "fixed", 
       "top": "45px", 
       "right": "0px" 
      }); 
     } else if (direction === "up") { 
      $(this).children(".sticky-items").removeAttr("style"); 
     } 
    }; 

$(document).ready(function() { 
    $('.entry-item').data('waypoint',true).waypoint(initwaypoint, { 
     offset: 5 
    }); 
    window.addmore = function() { 
     var $newdiv1 = $('<div class="entry-item"><div class="sticky-items">STICK XX</div></div><div><div class="entry-item"><div class="sticky-items">STICK XX</div><div>'); 
    $('#entries').append($newdiv1); 
     $('.entry-item').filter(function(){ 
      return !$(this).data('waypoint') 
     }).data('waypoint',true).waypoint(initwaypoint, { 
     offset: 5 
    }); 

} 
}); 
+0

您的演示代码是不工作 –

+0

@AhmedDousa THX!这是更新jsFiddle:http://jsfiddle.net/VxqNa/24/该问题是关于waypoints插件链接被删除 –