swiper.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var _imgid =0;
  2. var _dataid =0;
  3. var _datahid =0;
  4. // 相册
  5. $('.y_tuku_ul ul.y_tuku_ul_tu li').on('click',function(){
  6. // 索引第几张图片
  7. _imgid = $(this).index();
  8. _dataid = $(this).attr('data-id');
  9. _datahid = $(this).attr('data-hid');
  10. lpindex_xc(_imgid);
  11. })
  12. function lpindex_xc(imgid){
  13. $.post('/json/albumlist',{album_id:_dataid,hid:_datahid,csrf_token_f:csrfToken},function (data) {
  14. var data=data.data;
  15. var html = '';
  16. html+= '<div class="pc_slide_bj"></div>';
  17. html+= '<div class="view">';
  18. html+= '<div class="pc_slide_gb"><img src="/overseas/img/ico_colke.png" alt=""></div>';
  19. // html+= '<div class="pc_slide_fd"><img src="/image/swiper/ico_colke.png" alt=""></div>';
  20. html+= '<div class="swiper-container">';
  21. if(data.length != 1){
  22. html+= '<a class="arrow-left" href="#"></a>';
  23. html+= '<a class="arrow-right" href="#"></a>';
  24. }
  25. html+= '<div class="swiper-wrapper">';
  26. $.each(data,function(key,val){
  27. html+= '<div class="swiper-slide">';
  28. html+= '<div class="swiper_box">';
  29. html+= '<div class="swiper-zoom-container"><img class="" src="'+val.img+'" alt=""></div>';
  30. // html+= '<div class="swiper-zoom-container"><img class="" src="/overseas/img/img_5.png" alt=""></div>';
  31. html+= '</div>';
  32. html+= '</div>';
  33. });
  34. html+= '</div>';
  35. html+= '</div>';
  36. html+= '</div>';
  37. if(data.length != 1){
  38. html+= '<div class="preview_box">';
  39. // html+= '<div class="preview_text"><span>'+data[0].title+'</span></div>';
  40. html+= '<div class="preview">';
  41. html+= '<a class="arrow-left" href="#"></a>';
  42. html+= '<a class="arrow-right" href="#"></a>';
  43. html+= '<div class="swiper-container">';
  44. html+= '<div class="swiper-wrapper">';
  45. $.each(data,function(key,val){
  46. html+= '<div class="swiper-slide">';
  47. // html+= '<img src="/overseas/img/img_5.png" alt="">';
  48. html+= '<img src="'+val.img+'" alt="">';
  49. html+= '</div>';
  50. });
  51. html+= '</div>';
  52. html+= '</div>';
  53. html+= '</div>';
  54. html+= '</div>';
  55. }
  56. $('.pc-slide').html(html);
  57. $('.pc-slide').show();
  58. var _width = $('.preview').innerWidth();
  59. $('.preview').css('margin-left',-_width/2);
  60. $('.preview').show();
  61. $('.preview .swiper-wrapper .swiper-slide').eq(imgid).addClass('active-nav'); //默认选中
  62. // 放大功能
  63. function zoomImg(o) {
  64. var zoom = parseInt(o.style.zoom, 10) || 100;
  65. zoom += event.wheelDelta / 12; //可适合修改
  66. // console.log(zoom);
  67. if (zoom < 310 && zoom > 50 ) o.style.zoom = zoom + '%';
  68. }
  69. $(document).ready(function() {
  70. $(".view .swiper_box img").bind("mousewheel",
  71. function() {
  72. zoomImg(this);
  73. return false;
  74. });
  75. });
  76. // $(".pc_slide_fd").on('click',function(){
  77. // var style = $('.view .swiper-slide-active .swiper_box img').attr('style');
  78. // console.log(style);
  79. // $('.view .swiper-slide-active .swiper_box img').css('zoom','100%');
  80. // })
  81. $(".view .swiper_box img").dblclick(function(){
  82. $(this).css('zoom','100%');
  83. });
  84. var viewSwiper = new Swiper('.view .swiper-container', {
  85. onlyExternal : true,
  86. centeredSlides : true,
  87. initialSlide :imgid,
  88. onSlideChangeStart: function() {
  89. updateNavPosition(imgid);
  90. }
  91. })
  92. $('.view .arrow-left,.preview .arrow-left').on('click', function(e) {
  93. $('.view .swiper_box img').css('zoom','100%');
  94. e.preventDefault()
  95. if (viewSwiper.activeIndex == 0) {
  96. viewSwiper.slideTo(viewSwiper.slides.length - 1, 1000);
  97. return
  98. }
  99. viewSwiper.slidePrev()
  100. })
  101. $('.view .arrow-right,.preview .arrow-right').on('click', function(e) {
  102. $('.view .swiper_box img').css('zoom','100%');
  103. e.preventDefault()
  104. if (viewSwiper.activeIndex == viewSwiper.slides.length - 1) {
  105. viewSwiper.slideTo(0, 1000);
  106. return
  107. }
  108. viewSwiper.slideNext()
  109. })
  110. var previewSwiper = new Swiper('.preview .swiper-container', {
  111. //visibilityFullFit: true,
  112. slidesPerView: 'auto',
  113. allowTouchMove: false,
  114. onTap: function() {
  115. viewSwiper.slideTo(previewSwiper.clickedIndex)
  116. $('.view .swiper_box img').css('zoom','100%');
  117. }
  118. })
  119. function updateNavPosition(imgid) {
  120. if(imgid == 0){
  121. $('.preview .active-nav').removeClass('active-nav')
  122. var activeNav = $('.preview .swiper-slide').eq(viewSwiper.activeIndex).addClass('active-nav');
  123. if (!activeNav.hasClass('swiper-slide-visible')) {
  124. if (activeNav.index() > previewSwiper.activeIndex) {
  125. var thumbsPerNav = Math.floor(previewSwiper.width / activeNav.width()) - 1
  126. previewSwiper.slideTo(activeNav.index() - thumbsPerNav)
  127. } else {
  128. previewSwiper.slideTo(activeNav.index())
  129. }
  130. }
  131. }
  132. }
  133. $('.pc_slide_gb').on('click',function(){
  134. $('.pc-slide').hide();
  135. $('.pc-slide').html('');
  136. })
  137. imgid=0;
  138. })
  139. }