PHPIN.NET

 找回密码
 立即注册
查看: 6531|回复: 0

[jQuery/Js/AJAX] JQuery实现的页面滚动时浮动窗口控件:ScrollFollow

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2014-5-12 18:49:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
JQuery实现的页面滚动时浮动窗口控件:Scroll Follow
关键字词:Jquery Scrolling --Scroll Follow,浮动窗口控件,JQuery右侧跟随,JS右侧跟随
说起jquery的随窗口滚动的插件到是也不在少数,而且代码也不是那么复杂(对懂代码的人来说)。大部分的功能都是滚动到特定内容时,特定内容离顶端一定距离保持固定,功能上比较单一,有发现更好的滚动效果。

前一段时间看一淘网时发现侧边的导航的滚动效果很不错,除具有上面的效果外当接近页面底部某个特定版块又随页面一起滚动的特效,但这代码是用kissy写的不是jquery。如果是懂js的朋友我想写起来应该也不难,但咱不是啊,只能网上找了。

功夫不负有心人啊,终于发现了一个类似的效果,果断保存了下来。分享给大家,并自己稍作修改。朋友们如果发现有更好的可以留言分享下。

代码说明:
1、引入Scroll Follow插件
2、
  1. $("#sidebar ul").scrollFollow({ //要滚动的元素
  2. bottomObj: '#footer',  //底部元素
  3. marginTop: 8,  //距离顶端距离
  4. marginBottom: 0  //距离底部元素距离
  5. })
复制代码


以下为案例详情:
scrollFollow.js:
  1. $.fn.extend({
  2. scrollFollow: function(d) {
  3. d = d || {};
  4. d.container = d.container || $(this).parent();
  5. d.bottomObj = d.bottomObj || '';
  6. d.bottomMargin = d.bottomMargin || 0;
  7. d.marginTop = d.marginTop || 0;
  8. d.marginBottom = d.marginBottom || 0;
  9. d.zindex = d.zindex || 9999;
  10. var e = $(window);
  11. var f = $(this);
  12. if (f.length <= 0) {
  13. return false
  14. }
  15. var g = f.position().top;
  16. var h = d.container.height();
  17. var i = f.css("position");
  18. if (d.bottomObj == '' || $(d.bottomObj).length <= 0) {
  19. var j = false
  20. } else {
  21. var j = true
  22. }
  23. e.scroll(function(a) {
  24. var b = f.height();
  25. if (f.css("position") == i) {
  26. g = f.position().top
  27. }
  28. scrollTop = e.scrollTop();
  29. topPosition = Math.max(0, g - scrollTop);
  30. if (j == true) {
  31. var c = $(d.bottomObj).position().top - d.marginBottom - d.marginTop;
  32. topPosition = Math.min(topPosition, (c - scrollTop) - b)
  33. }
  34. if (scrollTop > g) {
  35. if (j == true && (g + b > c)) {
  36. f.css({
  37. position: i,
  38. top: g
  39. })
  40. } else {
  41. if (window.XMLHttpRequest) {
  42. f.css({
  43. position: "fixed",
  44. top: topPosition + d.marginTop,
  45. 'z-index': d.zindex
  46. })
  47. } else {
  48. f.css({
  49. position: "absolute",
  50. top: scrollTop + topPosition + d.marginTop + 'px',
  51. 'z-index': d.zindex
  52. })
  53. }
  54. }
  55. } else {
  56. f.css({
  57. position: i,
  58. top: g
  59. })
  60. }
  61. });
  62. return this
  63. }
  64. });
复制代码


DEMO页面代码:
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <style type="text/css">
  7. * { margin: 0; padding: 0; }
  8. body { font: 14px/1.4 Georgia, serif; background:#F2F2F2;}
  9. #page-wrap { width: 600px; margin:15px auto; }
  10. #header{padding:30px 0;border-bottom:2px solid #F27B03;}
  11. p { margin: 0 0 15px 0; font:400 14px/25px "Arial";}
  12. #main {padding:20px 20px 20px 0;width: 380px; float: left; }
  13. #sidebar { width: 200px; float: right;}
  14. #sidebar ul {border-left:1px solid #D5D5D5;padding-left:15px;}
  15. #sidebar ul li {line-height:30px;margin: 0 0 0 20px; }
  16. #footer{clear:both;border-top:2px solid #D5D5D5;padding:10px 0;}
  17. </style>
  18. </head>
  19. <body>
  20. <div id="page-wrap">        
  21. <div id="header">
  22. <h1>Jquery Scrolling --Scroll Follow</h1>
  23. <p>By <a href=""><strong>wmtimes</strong></a></p>
  24. </div>
  25. <div id="container">
  26. <div id="main">
  27. <h3>Scroll down and watch the sidebar on the right follow.</h3>
  28. <p>Opera mini and IE below version 10 don’t support any flexbox syntax. However, you can use the Flexie polyfill to add support for IE. Flexie uses the old flexbox syntax, but it does support IE as far back as version 6. Flexbox deserves its own article to be explained in detail, so I’ll point you to some articles I’ve written showing the old syntax and the new syntax, as well as one that walks you through the new syntax to set up a responsive layout that overcomes the issue of source order.Opera mini and IE below version 10 don’t support any flexbox syntax. However, you can use the Flexie polyfill to add support for IE. Flexie uses the old flexbox syntax, but it does support IE as far back as version 6. Flexbox deserves its own article to be explained in detail, so I’ll point you to some articles I’ve written showing the old syntax and the new syntax, as well as one that walks you through the new syntax to set up a responsive layout that overcomes the issue of source order.</p>
  29. <p>Opera mini and IE below version 10 don’t support any flexbox syntax. However, you can use the Flexie polyfill to add support for IE. Flexie uses the old flexbox syntax, but it does support IE as far back as version 6. Flexbox deserves its own article to be explained in detail, so I’ll point you to some articles I’ve written showing the old syntax and the new syntax, as well as one that walks you through the new syntax to set up a responsive layout that overcomes the issue of source order.Opera mini and IE below version 10 don’t support any flexbox syntax. However, you can use the Flexie polyfill to add support for IE. Flexie uses the old flexbox syntax, but it does support IE as far back as version 6. Flexbox deserves its own article to be explained in detail, so I’ll point you to some articles I’ve written showing the old syntax and the new syntax, as well as one that walks you through the new syntax to set up a responsive layout that overcomes the issue of source order.</p>
  30. <p>Suffice it to say that with a single CSS property, we can essentially tell our documents to display blocks of content in a different order than how the source code orders the blocks in the HTML. Jordan Moore has also written about flexbox and content choreography, and he’s created a demo to illustrate.</p>
  31. </div>
  32. <div id="sidebar">
  33. <ul>
  34. <li><a href="index.html">jQuery (animated)</a></li>
  35. <li><a href="css.html">CSS (fixed)</a></li>
  36. <li><a href="reveal.html">CSS (reveal)</a></li>
  37. <li><a href="index.html">jQuery (animated)</a></li>
  38. <li><a href="css.html">CSS (fixed)</a></li>
  39. <li><a href="reveal.html">CSS (reveal)</a></li>
  40. <li><a href="index.html">jQuery (animated)</a></li>
  41. <li><a href="css.html">CSS (fixed)</a></li>
  42. <li><a href="reveal.html">CSS (reveal)</a></li>
  43. <li><a href="index.html">jQuery (animated)</a></li>
  44. <li><a href="css.html">CSS (fixed)</a></li>
  45. <li><a href="reveal.html">CSS (reveal)</a></li>
  46. <li><a href="index.html">jQuery (animated)</a></li>
  47. <li><a href="css.html">CSS (fixed)</a></li>
  48. <li><a href="reveal.html">CSS (reveal)</a></li>
  49. <li><a href="css.html">CSS (fixed)</a></li>
  50. <li><a href="reveal.html">CSS (reveal)</a></li>
  51. <li><a href="index.html">jQuery (animated)</a></li>
  52. <li><a href="css.html">CSS (fixed)</a></li>
  53. <li><a href="reveal.html">CSS (reveal)</a></li>
  54. </ul>
  55. </div>
  56. </div>
  57. <div id="footer">
  58. <p>Other elements in the first column may relocate themselves to fill the now vacated space, but elements in the second column won’t. Even positioned elements are positioned relative to some parent, although that parent might be the html element itself. When you absolutely position an element and move it somewhere on the screen, other elements won’t get out of the way. We need them to get out of the way, though, if we’re going to intermix page elements.Other elements in the first column may relocate themselves to fill the now vacated space, but elements in the second column won’t. Even positioned elements are positioned relative to some parent, although that parent might be the html element itself. When you absolutely position an element and move it somewhere on the screen, other elements won’t get out of the way. We need them to get out of the way, though, if we’re going to intermix page elements.</p>
  59. <p>Other elements in the first column may relocate themselves to fill the now vacated space, but elements in the second column won’t. Even positioned elements are positioned relative to some parent, although that parent might be the html element itself. When you absolutely position an element and move it somewhere on the screen, other elements won’t get out of the way. We need them to get out of the way, though, if we’re going to intermix page elements.Other elements in the first column may relocate themselves to fill the now vacated space, but elements in the second column won’t. Even positioned elements are positioned relative to some parent, although that parent might be the html element itself. When you absolutely position an element and move it somewhere on the screen, other elements won’t get out of the way. We need them to get out of the way, though, if we’re going to intermix page elements.</p>
  60. </div>
  61. </div>
  62. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
  63. <script src="scrollFollow.js"></script>
  64. <script>
  65. $(document).ready(function() {
  66. $("#sidebar ul").scrollFollow({
  67. bottomObj: '#footer',
  68. marginTop: 8,
  69. marginBottom: 0
  70. })
  71. });
  72. </script>
  73. </body>
  74. </html>
复制代码


DEMO下载:
DEMO.rar (2.03 KB, 下载次数: 13)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-25 22:32

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表