dwz.scrollCenter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @desc 兼容不同的浏览器居中scrollCenter
  3. * @author ZhangHuihua@msn.com
  4. */
  5. (function($){
  6. $.fn.extend({
  7. getWindowSize: function(){
  8. if ($.browser.opera) { return { width: window.innerWidth, height: window.innerHeight }; }
  9. return { width: $(window).width(), height: $(window).height() };
  10. },
  11. /**
  12. * @param options
  13. */
  14. scrollCenter: function(options){
  15. // 扩展参数
  16. var op = $.extend({ z: 1000000, mode:"WH"}, options);
  17. // 追加到 document.body 并设置其样式
  18. var windowSize = this.getWindowSize();
  19. return this.each(function(){
  20. var $this = $(this).css({
  21. 'position': 'absolute',
  22. 'z-index': op.z
  23. });
  24. // 当前位置参数
  25. var bodyScrollTop = $(document).scrollTop();
  26. var bodyScrollLeft = $(document).scrollLeft();
  27. var movedivTop = (windowSize.height - $this.height()) / 2 + bodyScrollTop;
  28. var movedivLeft = (windowSize.width - $this.width()) / 2 + bodyScrollLeft;
  29. if (op.mode == "W") {
  30. $this.appendTo(document.body).css({
  31. 'left': movedivLeft + 'px'
  32. });
  33. } else if (op.model == "H"){
  34. $this.appendTo(document.body).css({
  35. 'top': movedivTop + 'px'
  36. });
  37. } else {
  38. $this.appendTo(document.body).css({
  39. 'top': (windowSize.height - $this.height()) / 2 + $(window).scrollTop() + 'px',
  40. 'left': movedivLeft + 'px'
  41. });
  42. }
  43. // 滚动事件
  44. $(window).scroll(function(e){
  45. var windowSize = $this.getWindowSize();
  46. var tmpBodyScrollTop = $(document).scrollTop();
  47. var tmpBodyScrollLeft = $(document).scrollLeft();
  48. movedivTop += tmpBodyScrollTop - bodyScrollTop;
  49. movedivLeft += tmpBodyScrollLeft - bodyScrollLeft;
  50. bodyScrollTop = tmpBodyScrollTop;
  51. bodyScrollLeft = tmpBodyScrollLeft;
  52. // 以动画方式进行移动
  53. if (op.mode == "W") {
  54. $this.stop().animate({
  55. 'left': movedivLeft + 'px'
  56. });
  57. } else if (op.mode == "H") {
  58. $this.stop().animate({
  59. 'top': movedivTop + 'px'
  60. });
  61. } else {
  62. $this.stop().animate({
  63. 'top': movedivTop + 'px',
  64. 'left': movedivLeft + 'px'
  65. });
  66. }
  67. });
  68. // 窗口大小重设事件
  69. $(window).resize(function(){
  70. var windowSize = $this.getWindowSize();
  71. movedivTop = (windowSize.height - $this.height()) / 2 + $(document).scrollTop();
  72. movedivLeft = (windowSize.width - $this.width()) / 2 + $(document).scrollLeft();
  73. if (op.mode == "W") {
  74. $this.stop().animate({
  75. 'left': movedivLeft + 'px'
  76. });
  77. } else if (op.mode == "H") {
  78. $this.stop().animate({
  79. 'top': movedivTop + 'px'
  80. });
  81. } else {
  82. $this.stop().animate({
  83. 'top': movedivTop + 'px',
  84. 'left': movedivLeft + 'px'
  85. });
  86. }
  87. });
  88. });
  89. }
  90. });
  91. })(jQuery);