jquery.fixed.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /****************************************************************
  2. * *
  3. * 代码库 *
  4. * www.dmaku.com *
  5. * 努力创建完善、持续更新插件以及模板 *
  6. * *
  7. ****************************************************************/
  8. /*
  9. * jQuery Fixed Plugins 1.5.1
  10. * Author:
  11. * Url:
  12. * Data
  13. *
  14. * Update Log:
  15. *
  16. * Status Date Name Version BUG-Description
  17. * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  18. * Created 2012-08-15 Ru 1.0 None
  19. * Modified 2012-09-02 Ru 1.4.1 修复了webkit内核浏览器右边浮动有一定距离的bug(负外边距),增加了悬浮靠边的定位、是否显示关闭按钮、是否垂直居中定位
  20. * Modified 2013-01-02 Ru 1.5.1 增加了垂直方向的位置;把核心函数(关闭、展开、定位、最小化)重构,修复了webkit内核浏览器右边浮动最小化时没有显示出来
  21. * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  22. ;(function($){
  23. $.fn.fixed = function(options){
  24. var defaults = {
  25. top : null,
  26. halfTop : false,
  27. durationTime : 500
  28. }
  29. var options = $.extend(defaults, options);
  30. this.each(function(){
  31. var thisBox = $(this),
  32. contentHeight = thisBox.height(),
  33. boxTop = null,
  34. defaultTop = thisBox.offset().top,
  35. halfTop = ($(window).height() - contentHeight)/2
  36. ;
  37. if(options.top == null){
  38. boxTop = defaultTop;
  39. }else {
  40. boxTop = options.top;
  41. }
  42. if( options.halfTop ) { boxTop = halfTop; }
  43. thisBox.css("top", boxTop);
  44. //核心scroll事件
  45. $(window).bind("scroll",function(){
  46. var offsetTop = boxTop + $(window).scrollTop() + "px";
  47. thisBox.animate({
  48. top: offsetTop
  49. },{
  50. duration: options.durationTime,
  51. queue: false
  52. });
  53. });
  54. }); //end this.each
  55. };
  56. })(jQuery);