【问题标题】:external html and jquery外部 html 和 jquery
【发布时间】:2012-07-10 20:35:51
【问题描述】:

我在 SO 上找到了这个脚本,我一直在尝试将它实现到我的项目中,但是我还是 jQuery 新手,需要一些帮助。

这是脚本:http://jsfiddle.net/9erk2/

我正在尝试获得这种效果,但是在外部加载的 html div 部分上,它不起作用。

我认为这与加载时不存在 div 的事实有关,因此我需要使用 .live() 方法。是这样吗,我该怎么做。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 你是什么意思?
  • @CoreyRS 我的意思是,我试图让侧边栏在到达窗口顶部时将其位置更改为固定。但由于这是在外部加载的 html 部分中发生的,我想我需要更改代码以包含 .live() 方法。

标签: jquery ajax css-position


【解决方案1】:

结合 fiddle 中提供的 css 和 html 代码试试这个。

<script>
    $window.delegate("#divID", "scroll", function(event){
        scrollTop = $window.scrollTop(),
        topPosition = Math.max(0, sidebarTop - scrollTop),
        topPosition = Math.min(topPosition, (footerTop - scrollTop) - sidebarHeight);
        $sidebar.css('top', topPosition);
    });
</script>

【讨论】:

  • 不走运。你想做什么?
  • 委托事件将函数绑定到页面上现在和将来的所有匹配元素,所以我正在查看它是否会将事件附加到动态加载的页面上。跨度>
  • 谢谢,我一直在阅读它,但这似乎不起作用。谁能告诉我为什么? ` $window = $(window), $sidebar = $(".sidebar"), sidebarTop = $sidebar.position().top, sidebarHeight = $sidebar.height(), $footer = $(".footer") , footerTop = $footer.position().top, $sidebar.addClass('fixed'); $window.on("scroll", function(event) { scrollTop = $window.scrollTop(), topPosition = Math.max(0, sidebarTop - scrollTop), topPosition = Math.min(topPosition, (footerTop - scrollTop) - sidebarHeight); $sidebar.css('top', topPosition); });`
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
相关资源
最近更新 更多