mag.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. show //正常状态的框
  3. bigshow // 放大的框的盒子
  4. smallshow //缩小版的框
  5. mask //放大的区域(黑色遮罩)
  6. bigitem //放大的框
  7. */
  8. // var obj = new mag('.show', '.bigshow','.smallshow','.mask','.bigitem');
  9. // obj.init()
  10. function mag(show, bigshow,smallshow,mask,bigitem) {
  11. this.show = show;
  12. this.bigshow = bigshow;
  13. this.smallshow = smallshow;
  14. this.mask = mask;
  15. this.bigitem = bigitem;
  16. this.obj = {
  17. prev: '.prev',
  18. next: '.next',
  19. middle: '.middle',
  20. middleBox: '.middle_box'
  21. }
  22. }
  23. mag.prototype = {
  24. init: function () {
  25. var that = this;
  26. that.start();
  27. this.showHover();
  28. this.smallImgHover();
  29. this.showMove();
  30. this.prevClick();
  31. this.nextClick();
  32. },
  33. start: function () {
  34. var that = this;
  35. var buil=$(that.show).width()/$(that.mask).width()*$(that.bigshow).width();
  36. $(that.bigitem).css("width",buil);
  37. $(that.smallshow + ' img').eq(0).css("border","2px solid #f40");
  38. var midhei=$(that.obj.middle + ' li').innerWidth()*$(that.obj.middle + ' li').length;
  39. $(that.obj.middle).width(midhei);
  40. },
  41. showHover: function () {
  42. var that = this;
  43. $(that.show).hover(function(){
  44. $(that.bigshow).show();
  45. $(that.mask).show();
  46. },function(){
  47. $(that.bigshow).hide();
  48. $(that.mask).hide();
  49. });
  50. },
  51. smallImgHover: function () {
  52. var that = this;
  53. $(that.smallshow + ' img').click(function(){
  54. var src=$(this).attr("src");
  55. $(that.smallshow + ' img').css("border","1px solid #e8e8e8");
  56. $(this).css("border","2px solid #f40");
  57. $(that.show + '>img').attr("src",src);
  58. $(that.bigitem+ '>img').attr("src",src);
  59. });
  60. },
  61. showMove:function(){
  62. var that = this;
  63. $(that.show).mousemove(function(e){
  64. var bigx=$(this).offset().left;
  65. var bigy=$(this).offset().top;
  66. var x= e.clientX;
  67. var y= e.clientY;
  68. var scrollx=$(window).scrollLeft();
  69. var scrolly=$(window).scrollTop();
  70. var ox=x+scrollx-bigx-$(that.mask).width()/2;
  71. var oy=y+scrolly-bigy-$(that.mask).height()/2;
  72. if(ox<=0){
  73. ox=0
  74. }
  75. if(ox>$(that.show).width()-$(that.mask).width()){
  76. ox=$(that.show).width()-$(that.mask).width();
  77. }
  78. if(oy<=0){
  79. oy=0
  80. }
  81. if(oy>$(that.show).height()-$(that.mask).height()){
  82. oy=$(that.show).height()-$(that.mask).height();
  83. }
  84. $(that.mask).css({left:ox});
  85. $(that.mask).css({top:oy});
  86. var bei=$(that.show).width()/$(that.mask).width();
  87. $(that.bigitem+ '>img').css(
  88. { marginLeft:-bei*ox,
  89. marginTop:-bei*oy
  90. })
  91. });
  92. },
  93. prevClick: function () {
  94. var that = this;
  95. $(that.obj.prev).click(function(){
  96. if($(that.obj.middle).width()-$(that.obj.middleBox).width()>0){
  97. if(Math.abs(parseInt($(that.obj.middle).css("marginLeft")))>$(that.obj.middleBox).width()){
  98. $(that.obj.middle).css("marginLeft",parseInt($(that.obj.middle).css("marginLeft"))+$(that.obj.middleBox).width())
  99. }
  100. if(Math.abs(parseInt($(that.obj.middle).css("marginLeft")))<$(that.obj.middleBox).width()){
  101. $(that.obj.middle).css("marginLeft","0px");
  102. $(that.obj.next).removeClass("nextnone");
  103. $(that.obj.prev).addClass("prevnone");
  104. }
  105. }else{
  106. return;
  107. }
  108. });
  109. },
  110. nextClick: function () {
  111. var that = this;
  112. $(that.obj.next).click(function(){
  113. if($(that.obj.middle).width()-$(that.obj.middleBox).width()>0){
  114. var shuzi=$(that.obj.middle).width()-Math.abs(parseInt($(that.obj.middle).css("marginLeft")))-$(that.obj.middleBox).width();
  115. if(shuzi>$(that.obj.middleBox).width()){
  116. $(that.obj.middle).css("marginLeft",-$(that.obj.middleBox).width()+parseInt($(that.obj.middle).css("marginLeft")))
  117. }
  118. if(shuzi<$(that.obj.middleBox).width()){
  119. $(that.obj.middle).css("marginLeft",-($(that.obj.middle).width()-$(that.obj.middleBox).width()))
  120. $(that.obj.next).addClass("nextnone");
  121. $(that.obj.prev).removeClass("prevnone");
  122. }
  123. }else{
  124. return;
  125. }
  126. });
  127. }
  128. }