jquery.capacityFixed.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. *
  3. * @author 愚人码头
  4. * @update yingp
  5. * 转载自 愚人码头-web前端开发-【jQuery插件】capacityFixed-类似于新浪微博新消息提示的定位框的实例页面
  6. * 类似于新浪微博新消息提示的定位框
  7. * 更多
  8. */
  9. (function($){
  10. $.fn.capacityFixed = function(options) {
  11. var opts = $.extend({},$.fn.capacityFixed.deflunt,options);
  12. var FixedFun = function(element) {
  13. var top = opts.top;
  14. var right = ($(window).width()-opts.pageWidth)/2+opts.right;
  15. element.css({
  16. "right":right,
  17. "top":top
  18. });
  19. $(window).resize(function(){
  20. element.css({
  21. "right":right
  22. });
  23. });
  24. $(window).scroll(function() {
  25. var scrolls = $(this).scrollTop();
  26. if (scrolls > top) {
  27. if (window.XMLHttpRequest) {
  28. element.css({
  29. position: "fixed",
  30. top: 0
  31. });
  32. } else {
  33. element.css({
  34. top: scrolls
  35. });
  36. }
  37. }else {
  38. element.css({
  39. position: "absolute",
  40. top: top
  41. });
  42. }
  43. });
  44. element.find(".close-ico").click(function(event){
  45. element.remove();
  46. event.preventDefault();
  47. });
  48. };
  49. return $(this).each(function() {
  50. FixedFun($(this));
  51. });
  52. };
  53. $.fn.capacityFixed.deflunt={
  54. right : 100,//相对于页面宽度的右边定位
  55. top:100,
  56. pageWidth : 960
  57. };
  58. })(jQuery);