转载来源:https://www.jb51.net/article/98030.htm

这是一个非常好的案例,然而jquery的时代正在徐徐关闭。

当你调整浏览器宽高,你会发现它不是自适应的。当你想把它放到elementUI项目中是,你发现不行!

如果你项目采用了vue和elementUI,建议以找找vue实现版本,或者单纯js版本。

本文实例为大家分享了js图片浏览效果的具体代码,供大家参考,具体内容如下

在线地址:http://www.hui12.com/nbin/demo/imgskim/index.html
https://nbin2008.github.io/demo/imgskim/index.html

index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>仿百度图片浏览</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <script src="js/jquery-2.1.0.js"></script>
    <script src="js/data.js"></script>
    <script src="js/handleImage.js"></script>
    <script src="js/index.js"></script>
  </head>
  <body>
    <div class="container1">
      <div class="main1">
        <!-- 图片显示 -->
        <div class="showImg1">
          <a href="javascript:;" class="showImg1_btnLeft"></a>
          <a href="javascript:;" class="showImg1_btnRight"></a>
          <div class="imgBox1">
            <img src="" class="img1"/>
          </div>
        </div>
        <!-- 图片选择 -->
        <div class="chooseImg1_box">
          <div class="navList1">
            <span class="btnImgList">图片列表<i></i></span>
            <span class="btnImgScale">
              <a href="javascript:;" class="scaleAdd1">+</a>
              <b class="scale1">100%</b>
              <a href="javascript:;" class="scaleReduce1">-</a>
            </span>
            <span class="btnImgInit1">原始尺寸</span>
            <span class="btnImgFullScreen">全屏</span>
            <span>其他</span>
            <span>其他</span>
          </div>
          <div class="boxLimit1">
            <a href="javascript:;" class="chooseImg1_btnLeft"></a>
            <div class="imgListBox1">
              <ul class="imgList1"></ul>
            </div>
            <a href="javascript:;" class="chooseImg1_btnRight"></a>
          </div>
            
        </div>
      </div>
      <div class="sider1">
        <p class="pTroTit1"></p>
        <p class="pTroName1"></p>
      </div>
    </div>
    <!-- 全屏 -->
    <div class="container2">
      <div class="chooseTimeBox">
        <select class="select">
          <option value="2">2s</option>
          <option value="3">3s</option>
          <option value="5">5s</option>
        </select>
        <a href="javascript:;" class="btnStart">开始</a>
        <a href="javascript:;" class="btnStop">||</a>
      </div>
      <!-- main -->
      <div class="imgBox2">
        <img src="" class="img2" />
      </div>
      <a href="javascript:;" class="showImg2_btnLeft"></a>
      <a href="javascript:;" class="showImg2_btnRight"></a>
      <div class="imgListBox2">
        <ul class="imgList2"></ul>
      </div>
      <a href="javascript:;" class="chooseImg2_btnLeft aBtn" ></a>
      <a href="javascript:;" class="chooseImg2_btnRight aBtn"></a>
      <a href="javascript:;" class="btnExitFullScreen">x</a>
    </div>
  </body>
</html>

css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* common */
*{
  margin: 0; padding: 0;
}
body,html{
  font-family: "微软雅黑"; font-size: 12px; overflow: hidden;
}
li{
  list-style: none;
}
a{
  text-decoration: none; color: #000;
}
.btnIco{
  background: url(../images/btn.png);
}
b{
  font-weight: normal;
}
i{
  font-style: normal;
}
  
/* container1 */
.container1{
  width: 100%; height: 100%; background-color: #f6f6f6; position: absolute; min-width: 1000px; min-height: 400px; -display: none;
}
.main1{
  position: absolute; width: calc(100% - 310px); height: calc(100% - 5px); left: 0; top: 5px; background-color: #fff;
}
.sider1{
  position: absolute; width: 300px; margin-left: 10px; height: calc(100% - 5px); overflow-x: hidden; overflow-y: auto; top: 5px; right: 0px; background-color: #fff;
}
.showImg1{
  width: 100%; position: relative;
}
.showImg1 a{
  position: absolute; width: 70px; height: 100%; top: 0; background-color: #fff; transition: all 0.5s;
}
.showImg1 a:hover{
  background-color: #e6e6e6;
}
.showImg1 a:before{
  content: ''; display: block; position: absolute; width: 40px; height: 72px; background: url(../images/btn.png); left: calc(50% - 20px); top: calc(50% - 31px);
}
.showImg1 .showImg1_btnLeft{
  left: 0;
}
.showImg1 .showImg1_btnRight{
  right: 0;
}
.showImg1 .showImg1_btnLeft:before{
  background-position: 0 -87px;
}
.showImg1 .showImg1_btnLeft:hover:before{
  background-position: -46px -87px;
}
.showImg1 .showImg1_btnRight:before{
  background-position: 0 0;
}
.showImg1 .showImg1_btnRight:hover:before{
  background-position: -46px 0;
}
.showImg1 .imgBox1{
  position: absolute; width: calc(100% - 144px); height: calc(100% - 4px); left: 72px; top: 2px; overflow: hidden;
}
.showImg1 .imgBox1 .img1{
  position: absolute; display: block;
}
.chooseImg1_box{
  position: relative; overflow: hidden;
}
.navList1{
  height: 38px; line-height: 38px; border-top: 1px solid #e3e3e3; border-bottom: 1px solid #e3e3e3; text-align: center; font-size: 14px;
}
.navList1 span{
  margin-left: -3px;
}
.navList1 span, .navList1 a{
  display: inline-block; color: #666; padding: 0 10px; cursor: pointer; position: relative;
}
.navList1 span:before{
  content: '|'; display: block; position: absolute; left: -3px; color: #e3e3e3;
}
.navList1 span:hover:before{
  display: none;
}
.navList1 span:last-child:after{
  content: '|'; display: block; position: absolute; right: 1px; color: #e3e3e3;
}
.navList1 span:hover, .navList1 a:hover{
  background-color: #e3e3e3;
}
.navList1 .btnImgScale, .navList1 .btnImgScale:hover{
  cursor: default; background-color: #fff; padding: 0;
}
.navList1 a{
  padding: 0; width: 30px;
}
  
.boxLimit1{
  position: relative; width: 100%; height: 100px
}
.boxLimit1 a{
  float: left; display: block; width: 25px; height: 70px; margin: 15px 2px 0; position: relative;
}
.boxLimit1 a:hover{
  background-color: #e6e6e6;
}
.boxLimit1 a.disable{
  background-color: #fff;
}
.boxLimit1 a:before{
  content: ''; display: block; position: absolute; width: 14px; height: 27px; left: calc(50% - 7px); top: calc(50% - 13px); background: url(../images/btn.png);
}
.boxLimit1 .chooseImg1_btnLeft:before, .boxLimit1 .chooseImg1_btnLeft.disable:hover:before{
  background-position: -27px -174px;
}
.boxLimit1 .chooseImg1_btnLeft.disable:hover:before{
  cursor: default;
}
.boxLimit1 .chooseImg1_btnLeft:hover:before{
  background-position: -73px -174px;
}
.boxLimit1 .chooseImg1_btnRight:before, .boxLimit1 .chooseImg1_btnRight.disable:hover:before{
  background-position: 0 -174px;
}
.boxLimit1 .chooseImg1_btnRight.disable:hover:before{
  cursor: default;
}
.boxLimit1 .chooseImg1_btnRight:hover:before{
  background-position: -46px -174px;
}
.imgListBox1{
  position: relative; width: calc(100% - 58px); height: 90px; padding: 4px 0 6px; float: left; overflow: hidden;
}
.imgListBox1 .imgList1{
  padding-top: 5px; position: absolute; left: 0; top: 0; transition: left 0.5s; float: left;
}
.imgListBox1 .imgList1 li{
  float: left; width: 68px; height: 68px; border: 1px solid #dfdfdf; margin-top: 10px; margin-right: 10px; background-image: url(../images/a2.jpg); background-repeat: no-repeat; background-position: center; background-size: contain; cursor: pointer;
}
.imgListBox1 .imgList1 li.active{
  width: 76px; height: 76px; border: 2px solid #3388fb; margin-top: 5px; background-image: url(../images/a1.jpg);
}
  
/* 全屏 */
.container2{
  width: 100%; height: 100%; background-color: #262626; position: absolute; display: none;
}
.btnExitFullScreen{
  color: #989898; position: absolute; top: 1%; right: 1%; font-size: 20px; line-height: 20px;
}
.chooseTimeBox{
  position: absolute; width: 70px; text-align: center; height: 20px; line-height: 20px; background-color: #3d3d3d; left: calc(50% - 35px); top: 1.5%;
}
.chooseTimeBox *{
  color: #e1e1e1;
}
.chooseTimeBox .select{
  background: #121212; color: #999999; width: 40px; height: 18px; left: 2px; top:1px; outline: none; display: none; float: left; position: relative; top: 1px;
}
.chooseTimeBox .btnStart{
  -display: none;
}
.chooseTimeBox .btnStop{
  display: none; position: relative; top: -1px;
}
.imgBox2{
  position: absolute; width: 70%; height: calc(90% - 144px); -border: 1px solid red; left: 15%; top: 8%;
}
.imgBox2 img{
  position: absolute;
}
.showImg2_btnLeft, .showImg2_btnRight{
  width: 50%; height: calc(90% - 144px); position: absolute; top: 8%; -border: 1px solid #fff;
}
.showImg2_btnLeft{
  left: 0; cursor: url(../images/cur_left.jpg),auto;;
}
.showImg2_btnRight{
  right: 0; cursor: url(../images/cur_right.jpg),auto;;
}
.imgListBox2{
  position: absolute; width: 80%; height: 140px; border: 1px solid #3e3e3e; left: 10%; bottom: 2%; overflow: hidden;
}
.imgList2{
  position: absolute; left: 0; top: 0; width: 100%; height: 110px; top: 15px; transition: left 0.5s;
}
.imgList2 li{
  width: 110px; height: 110px; box-sizing: border-box; border: 1px solid #707070; float: left; margin-right: 5px; background-image: url(../images/a2.jpg); background-repeat: no-repeat; background-position: center; background-size: contain; cursor: pointer;
}
.imgList2 li.active{
  border: 2px solid #2f95d5;
}
  
.container2 .aBtn{
  position: absolute; display: block; width: 25px; height: 70px; margin: 15px 2px 0;
}
.container2 .aBtn:hover{
  background-color: #e6e6e6;
}
.container2 .aBtn.disable{
  background-color: #fff;
}
.container2 .aBtn:before{
  content: ''; display: block; position: absolute; width: 14px; height: 27px; left: calc(50% - 7px); top: calc(50% - 13px); background: url(../images/btn.png);
}
.container2 .chooseImg2_btnLeft{
  left: calc(10% - 50px); bottom: calc(2% + 35px);
}
.container2 .chooseImg2_btnRight{
  right: calc(10% - 50px); bottom: calc(2% + 35px);
}
.container2 .chooseImg2_btnLeft:before, .container2 .chooseImg2_btnLeft.disable:hover:before{
  background-position: -27px -174px;
}
.container2 .chooseImg2_btnLeft.disable:hover:before{
  cursor: default;
}
.container2 .chooseImg2_btnLeft:hover:before{
  background-position: -73px -174px;
}
.container2 .chooseImg2_btnRight:before, .container2 .chooseImg2_btnRight.disable:hover:before{
  background-position: 0 -174px;
}
.container2 .chooseImg2_btnRight.disable:hover:before{
  cursor: default;
}
.container2 .chooseImg2_btnRight:hover:before{
  background-position: -46px -174px;
}

data.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var imgData = [
  {
    src: 'images/a1.jpg',
    title: 'a1.jpg',
    name: '火影忍着1'
  },
  {
    src: 'images/a2.jpg',
    title: 'a2.jpg',
    name: '火影忍着3'
  },
  {
    src: 'images/a3.jpg',
    title: 'a3.jpg',
    name: '火影忍着3'
  },
  {
    src: 'images/a4.jpg',
    title: 'a4.jpg',
    name: '火影忍着4'
  },
  {
    src: 'images/a5.jpg',
    title: 'a5.jpg',
    name: '火影忍着5'
  },
  {
    src: 'images/a6.jpg',
    title: 'a6.jpg',
    name: '火影忍着6'
  },
  {
    src: 'images/a7.jpg',
    title: 'a7.jpg',
    name: '火影忍着7'
  },
  {
    src: 'images/a8.jpg',
    title: 'a8.jpg',
    name: '火影忍着8'
  },
  {
    src: 'images/a9.jpg',
    title: 'a9.jpg',
    name: '火影忍着9'
  },
  {
    src: 'images/a10.jpg',
    title: 'a10.jpg',
    name: '火影忍着10'
  },
  {
    src: 'images/a11.jpg',
    title: 'a11.jpg',
    name: '火影忍着11'
  },
  {
    src: 'images/a12.jpg',
    title: 'a12.jpg',
    name: '火影忍着12'
  },
  {
    src: 'images/a13.jpg',
    title: 'a13.jpg',
    name: '火影忍着13'
  },
  {
    src: 'images/a14.jpg',
    title: 'a14.jpg',
    name: '火影忍着14'
  },
  {
    src: 'images/a15.jpg',
    title: 'a15.jpg',
    name: '火影忍着15'
  },
  {
    src: 'images/a16.jpg',
    title: 'a16.jpg',
    name: '火影忍着16'
  },
  {
    src: 'images/a17.jpg',
    title: 'a17.jpg',
    name: '火影忍着17'
  },
  {
    src: 'images/a18.jpg',
    title: 'a18.jpg',
    name: '火影忍着18'
  },
  {
    src: 'images/a19.jpg',
    title: 'a19.jpg',
    name: '火影忍着19'
  },
  {
    src: 'images/a20.jpg',
    title: 'a20.jpg',
    name: '火影忍着20'
  }
];

handleImage.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(function(window,$){
  function HandleImage(e){
    this.init(e);
  };
  var proto = {
    init: function(e){
      this.nb = {};
      this.nb.$box = e.box;
      this.nb.$img = e.img;
      this.setBoxWH();
      this.imgMouseEvent();
    },
    //对外提供,重置盒子的宽高,resize事件需要调用
    setBoxWH: function(){
      this.nb.bWidth = this.nb.$box.width();
      this.nb.bHeight = this.nb.$box.height();
    },
    getImgWH: function(src,isNormal,callback){
      var self = this;
      var img = new Image();
      img.onload = function(){
        self.nb.mWidth = img.width;
        self.nb.mHeight = img.height;
        self.setStartPosition(isNormal);
        callback && callback();
      };
      img.src = src;
    },
    //对外提供,输入图片地址,isNormal:true(初始不缩放)
    setImg: function(src,isNormal,callback){
      this.getImgWH(src,isNormal,callback);
      this.nb.$img.attr('src',src);
    },
    //初始化图片位置
    setStartPosition: function(isNormal){
      var self = this;
      var bW = self.nb.bWidth = self.nb.$box.width();
<span style="white-space:pre">        </span>bH = self.nb.bHeight = self.nb.$box.height();
        mW = self.nb.mWidth,
        mH = self.nb.mHeight;
      var sScale = self.nb.nScale = Math.min(bW/mW,bH/mH);
      if( sScale>=1 || isNormal ){
        self.nb.nScale = 1;
        self.nb.left = (bW-mW)/2;
        self.nb.top = (bH-mH)/2;
        self.nb.$img.css({
          'width': mW,
          'height': mH,
          'left': (bW-mW)/2,
          'top': (bH-mH)/2
        })
      }else{
        self.nb.left = (bW-mW*sScale)/2;
        self.nb.top = (bH-mH*sScale)/2;
        self.nb.$img.css({
          'width': mW*sScale,
          'height': mH*sScale,
          'left': (bW-mW*sScale)/2,
          'top': (bH-mH*sScale)/2
        })
      };
      this.setCenter();
    },
    setCenter: function(){
      var self = this;
      this.nb.centerX = self.nb.left + self.nb.mWidth*self.nb.nScale/2;
      this.nb.centerY = self.nb.top + self.nb.mHeight*self.nb.nScale/2;
    },
    //对外提供,改变图片大小
    setScale: function(str,callback){
      var self = this;
      if( str == 'plus'){
        self.nb.nScale += 0.1;
      }else if( str == 'reduce' ){
        self.nb.nScale -= 0.1;
      };
      self.nb.nScale = self.nb.nScale>=10?10:self.nb.nScale;
      self.nb.nScale = self.nb.nScale<=0.1?0.1:self.nb.nScale;
      self.nb.left = self.nb.centerX - self.nb.mWidth*self.nb.nScale/2;
      self.nb.top = self.nb.centerY - self.nb.mHeight*self.nb.nScale/2;
      self.nb.$img.css({
        'width': self.nb.mWidth*self.nb.nScale,
        'height': self.nb.mHeight*self.nb.nScale,
        'left': self.nb.left,
        'top': self.nb.top
      });
      callback && callback();
    },
    //对外提供,获取缩放比例
    getScale: function(){
      return this.nb.nScale;
    },
    imgMouseEvent: function(){
      var self = this;
      var sX,sY,disX,disY,sImgX,sImgY,b;
      self.nb.$img.on('mousedown',function(e){
        b = true;
        sX = e.pageX;
        sY = e.pageY;
        sImgX = self.nb.left;
        sImgY = self.nb.top;
      });
      $(document).on('mousemove',function(e){
        if( !b ) return;
        self.nb.$img.css({
          'left': sImgX + e.pageX - sX,
          'top': sImgY + e.pageY - sY
        });
        return false;
      });
      $(document).on('mouseup',function(){
        b = false;
        self.nb.left = parseInt(self.nb.$img.css('left'));
        self.nb.top = parseInt(self.nb.$img.css('top'));
        self.setCenter();
      });
    }
  };
  HandleImage.prototype = proto;
  window.HandleImage = HandleImage;
})(window,jQuery);

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
$(document).ready(function(){
  var $win = $(window),
    $con1 = $('.container1'),
    $main1 = $('.main1'),
    $showImg1 = $('.showImg1'),
    $showImg1_btnLeft = $('.showImg1_btnLeft'),
    $showImg1_btnRight = $('.showImg1_btnRight'),
    $imgBox1 = $('.imgBox1'),
    $img1 = $('.img1'),
    $showImg1_btnLeft = $('.showImg1_btnLeft'),
    $showImg1_btnRight = $('.showImg1_btnRight'),
    $chooseImg1_btnLeft = $('.chooseImg1_btnLeft'),
    $chooseImg1_btnRight = $('.chooseImg1_btnRight'),
    $chooseImg1_box = $('.chooseImg1_box'),
    $scale1 = $('.scale1'),
    $btnImgInit1 = $('.btnImgInit1'),
    $btnImgFullScreen = $('.btnImgFullScreen'),
      
    $sider1 = $('.sider1'),
    $imgListBox1 = $('.imgListBox1'),
    $imgList1 = $('.imgList1');
  //container2-fullscreen对象
  var $con2 = $('.container2'),
    $select = $('.select'),
    $btnStart = $('.btnStart'),
    $btnStop = $('.btnStop'),
    $btnExitFullScreen = $('.btnExitFullScreen'),
    $imgBox2 = $('.imgBox2'),
    $img2 = $('.img2'),
    $showImg2_btnLeft = $('.showImg2_btnLeft'),
    $showImg2_btnRight = $('.showImg2_btnRight'),
    $chooseImg2_btnLeft = $('.chooseImg2_btnLeft'),
    $chooseImg2_btnRight = $('.chooseImg2_btnRight'),
    $imgListBox2 = $('.imgListBox2'),
    $imgList2 = $('.imgList2');
      
  var winW = $win.width(),
    winH = $win.height();
  //图片处理事件
  var h1;
  var handle = {
    init1: function(){
      h1 = new HandleImage({
        box: $imgBox1,
        img: $img1
      });
    },
    setImg1: function(src,isNormal){
      h1.setImg(src,isNormal,function(){
        imgListEvent.setScaleText();
      });
    }
  };
    
  //窗口改变事件
  var envFunc = {
    fnSize: function(){
      $(window).on('resize',function(){
        winW = $win.width(),
        winH = $win.height();
        containEvent.setCon();
        containEvent.setShowImg1_height();
        imgListEvent.imgList1_position();
        h1.setBoxWH();
      });
    }
  };
  envFunc.fnSize();
    
  //cantanier事件
  var containEvent = {
    init: function(){
      this.setCon();
      this.setShowImg1_height();
      handle.init1();
    },
    //设置container宽高
    setCon: function(){
      $con1.css({
        'width': winW,
        'height': winH
      });
      $con2.css({
        'width': winW,
        'height': winH
      });
    },
    //设置图片控制区高
    setShowImg1_height: function(){
      $showImg1.css({
        'height': $main1.height() - $chooseImg1_box.height()
      })
    },
    showText: function(data){
      $('.pTroTit1').text(data['src']);
      $('.pTroName1').text(data['title']);
    }
  };
  containEvent.init();
    
  //图片选择 普通的width:70+10, 选中active:80+10
  var $liActive_1;
  var index = 0;
  var imgListEvent = {
    init: function(){
      this.imgList1_add();
      this.imgList1_click();
      this.scaleEvent();
      this.mouseWheelEvent();
      this.fnClick();
    },
    imgList1_add: function(){
      var str = ''
      for( var i=0; i<imgData.length; i++ ){
        var tmp = imgData[i];
        str += '<li style="background-image:url('+ tmp.src +')" ></li>'
      };
      $imgList1.append(str);
      $imgList1.css({
        'width': (70+10)*imgData.length + 10
      });
    },
    imgList1_click: function(){
      var self = this;
      $imgList1.find('li').on('click',function(){
        if( $liActive_1 ) $liActive_1.removeClass('active');
        index = $(this).index();
        $(this).addClass('active');
        $liActive_1 = $(this);
        self.imgList1_position();
        handle.setImg1( imgData[index]['src'] );
        containEvent.showText( imgData[index] );
      });
      $imgList1.find('li').eq(0).trigger('click');
    },
    imgList1_position: function(){
      var boxWidth1 = $imgListBox1.width();
      var le = (boxWidth1/2 - index*80);
      le = Math.floor(le/80)*80;
      le = le>=0?0:le;
      var maxLe = (boxWidth1-$imgList1.width())+10;
      le = le<maxLe?maxLe:le;
      $imgList1.css({
        'left': le
      })
    },
    scaleEvent: function(){
      var timer = null;
      var btnPos = {
        x: null,
        y: null
      };
      var fnCallback = function(){
        imgListEvent.setScaleText();
      };
      $('.scaleAdd1').on('mousedown',function(e){
        h1.setScale('plus',fnCallback);
        checkBtnPos(e);
        timer = setTimeout(function(){
          fnAnimate('plus');
        },500);
      });
      $('.scaleReduce1').on('mousedown',function(e){
        h1.setScale('reduce',fnCallback);
        checkBtnPos(e);
        timer = setTimeout(function(){
          fnAnimate('reduce');
        },500);
      });
      $('.scaleAdd1').add($('.scaleReduce1')).on('mouseup',function(){
        clearInterval(timer);
        return false;
      });
      $('.scaleAdd1').add($('.scaleReduce1')).on('mousemove',function(e){
        if( Math.abs(e.pageX-btnPos.x)>=5 || Math.abs(e.pageY-btnPos.y)>=5 ){
          clearInterval(timer);
        };
        return false;
      });
      function checkBtnPos(e){
        btnPos.x = e.pageX;
        btnPos.y = e.pageY;
      };
      function fnAnimate(str){
        if( str == 'plus' ){
          timer = setInterval(function(){
            h1.setScale('plus',fnCallback);
          },30);
        }else if( str == 'reduce'){
          timer = setInterval(function(){
            h1.setScale('reduce',fnCallback);
          },30)
        };
      };
    },
    mouseWheelEvent: function(){
      var imgBox1 = $imgBox1.get(0);
      if( document.attachEvent ){
        imgBox1.attachEvent('onmousewheel',move)
      };
      if( document.addEventListener ){
        imgBox1.addEventListener('mousewheel',move,false);
        imgBox1.addEventListener('mousewheel',move,false);
      };
      var fnCallback = function(){
        imgListEvent.setScaleText();
      };
      function move(e){
        var up = true;
        if( e.wheelDelta ){
          up = e.wheelDelta > 0 ? true : false;
        };
        if( e.detail ){
          up = e.detail < 0 ? true : false;
        };
        if( up ){
          h1.setScale('plus',fnCallback);
        }else{
          h1.setScale('reduce',fnCallback);
        };
        if( e.preventDefault ){
          e.preventDefault();
        }else{
          e.returnValue = false;
        }
      };
    },
    setScaleText: function(){
      var scale = Math.round(h1.getScale()*100);
      $scale1.text(scale+'%');
    },
    fnClick: function(){
      $showImg1_btnRight.on('click',function(){
        $liActive_1.next().trigger('click');
      });
      $showImg1_btnLeft.on('click',function(){
        $liActive_1.prev().trigger('click');
      });
      $chooseImg1_btnLeft.on('click',function(){
        var w = $imgListBox1.width();
        var le = parseInt($imgList1.css('left')) + w;
        if( le > 0 ) le = 0;
        $imgList1.css({
          'left': le
        })
      });
      $chooseImg1_btnRight.on('click',function(){
        var w = $imgListBox1.width();
        var minLe = w - $imgList1.width();
        var le = parseInt($imgList1.css('left')) - w;
        if( le < minLe ) le = minLe;
        $imgList1.css({
          'left': le
        })
      });
      $btnImgInit1.on('click',function(){
        handle.setImg1( imgData[index]['src'], true );
      });
      $btnImgFullScreen.on('click',function(){
        fullScreen.enterFull();
      });
    }
  };
  imgListEvent.init();
    
  /*
   * container2
   */
  var $liActive_2;
  var timer2;
  function setImg2(src){
    $imgBox2;
    $img2;
    var bW,bH,mW,mH;
    var img = new Image();
    $img2.attr('src',src);
    img.onload = function(){
      mW = img.width;
      mH = img.height;
      bW = $imgBox2.width();
      bH = $imgBox2.height();
      setPosition();
    };
    img.src = src;
    function setPosition(){
      var sScale = Math.min(bW/mW,bH/mH);
      if( sScale>=1 ){
        $img2.css({
          'width': mW,
          'height': mH,
          'left': (bW-mW)/2,
          'top': (bH-mH)/2
        });
      }else{
        $img2.css({
          'width': mW*sScale,
          'height': mH*sScale,
          'left': (bW-mW*sScale)/2,
          'top': (bH-mH*sScale)/2
        });
      };
    };
  };
  var fullScreen = {
    init: function(){
      this.addImg();
      this.addClick();
    },
    addImg: function(){
      var str = ''
      for( var i=0; i<imgData.length; i++ ){
        var tmp = imgData[i];
        str += '<li style="background-image:url('+ tmp.src +')" ></li>'
      };
      $imgList2.append(str);
      $imgList2.css({
        'width': 115*imgData.length
      });
    },
    addClick: function(){
      var self = this;
      $imgList2.find('li').on('click',function(){
        if( $liActive_2 ) $liActive_2.removeClass('active');
        index = $(this).index();
        $(this).addClass('active');
        $liActive_2 = $(this);
        self.imgList2_position();
        setImg2( imgData[index]['src'] );
      });
      $showImg2_btnRight.on('click',function(){
        $liActive_2.next().trigger('click');
      });
      $showImg2_btnLeft.on('click',function(){
        $liActive_2.prev().trigger('click');
      });
      $chooseImg2_btnLeft.on('click',function(){
        var w = $imgListBox2.width();
        var le = parseInt($imgList2.css('left')) + w;
        if( le > 0 ) le = 0;
        $imgList2.css({
          'left': le
        })
      });
      $chooseImg2_btnRight.on('click',function(){
        var w = $imgListBox2.width();
        var minLe = w - $imgList2.width();
        var le = parseInt($imgList2.css('left')) - w;
        if( le < minLe ) le = minLe;
        $imgList2.css({
          'left': le
        })
      });
      $btnExitFullScreen.on('click',function(){
        self.exitFull();
      });
    },
    imgList2_position: function(){
      var boxWidth2 = $imgListBox2.width();
      var le = (boxWidth2/2 - index*115);
      le = Math.floor(le/115)*115;
      le = le>=0?0:le;
      var maxLe = (boxWidth2-$imgList2.width());
      le = le<maxLe?maxLe:le;
      $imgList2.css({
        'left': le
      });
    },
    enterFull: function(){
      var self = this;
      enterFullscreen();
      $con1.css('opacity','0');
      $con2.show();
      setTimeout(function(){
        $imgList2.find('li').eq(index).trigger('click');
      },500);
      //esc keyCode:27
      $(document).on('keyup.a',function(e){
        if( e.keyCode == 27 ){
          self.exitFull();
        };
      });
    },
    exitFull: function(){
      clearInterval(timer2);
      $(document).off('keyup.a');
      $con1.css('opacity','1');
      $con2.hide();
      animateEvent.showStart();
      setTimeout(function(){
<span style="white-space:pre">        </span>$imgList1.find('li').eq(index).trigger('click');
<span style="white-space:pre">      </span>},500);
      exitFullscreen();
    }
  };
  fullScreen.init();
// fullScreen.enterFull();
    
  //定时器
  var animateEvent = {
    init: function(){
      var self = this;
      $btnStart.on('click',function(){
        self.start();
      });
      $btnStop.on('click',function(){
        self.stop();
      });
      $select.on('change',function(){
        self.start();
      });
    },
    start: function(){
      this.showStop();
      var intervalTime = parseInt($select.val())*1000;
      clearInterval(timer2);
      timer2 = setInterval(function(){
        $liActive_2.next().trigger('click');
      },intervalTime);
    },
    stop: function(){
      clearInterval(timer2);
      this.showStart();
    },
    showStart: function(){
      clearInterval(timer2);
      $select.show().val('2');
      $select.hide();
      $btnStop.hide();
      $btnStart.show();
    },
    showStop: function(){
      $btnStart.hide();
      $btnStop.show();
      $select.show();
    }
  };
  animateEvent.init();
    
  /*
   * 全屏事件
   */
  // 判断各种浏览器
  function enterFullscreen() {
    var element = document.documentElement;
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
      element.webkitRequestFullscreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    }
  }
  // 判断浏览器种类
  function exitFullscreen() {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
  }
});
 
 

JavaScript仿百度图片浏览效果(转载)的更多相关文章

  1. javascript仿新浪微博图片放大缩小及旋转效果

    javascript仿新浪微博图片放大缩小及旋转效果 经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以 ...

  2. jQuery仿迅雷图片轮换效果

    jQuery仿迅雷图片轮换效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  3. 仿百度图片首页--HTML+CSS练手项目1【Table】

    [本文为原创,转载请注明出处] 技术[CSS+HTML]   布局[Table] 图片准备[百度图标.10张不同类型图] --------------------------------------- ...

  4. Android 高级UI设计笔记06:仿微信图片选择器(转载)

    仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...

  5. javascript马赛克遮罩图片切换效果:XMosaic.js(转)

    新鲜出炉的javascript图片切换特效,实现的是马赛克遮罩切换.在flash里,好实现遮罩动画很简单,不过JS实现起来就有些困难了. XMosaic.js,与XScroll.js和XScroll2 ...

  6. 基于vue2.0实现仿百度前端分页效果(二)

    前言 上篇文章中,已经使用vue实现前端分页效果,这篇文章我们单独将分页抽离出来实现一个分页组件 先看实现效果图 代码实现 按照惯例,我们在冻手实现的时候还是先想一想vue实现组件的思路 1.需要提前 ...

  7. 基于vue2.0实现仿百度前端分页效果(一)

    前言 最近在接手一个后台管理项目的时候,由于之前是使用jquery+bootstrap做的,后端使用php yii框架,前后端耦合在一起,所以接手过来之后通过vue进行改造,但依然继续使用的boots ...

  8. 【javascript/css】Javascript+Css实现图片滑动浏览效果

    今天用js+css来做一个能够左右滑动的图片浏览效果. 首先写一个结构,包括需要浏览的两张图,以及能够点击来滑动图片的两个按钮. <!DOCTYPE html> <html> ...

  9. Android 滑动效果基础篇(三)—— Gallery仿图像集浏览

    Android系统自带一个Gallery浏览图片的应用,通过手指拖动时能够非常流畅的显示图片,用户交互和体验都很好. 本示例就是通过Gallery和自定义的View,模仿实现一个仿Gallery图像集 ...

随机推荐

  1. 使用MSCK命令修复Hive表分区

    set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...

  2. git clone速度太慢的解决办法(亲测还有效)

    https://www.linuxidc.com/Linux/2019-05/158461.htm 1.查找域名对应的ip地址,并修改hosts文件 linuxidc@linuxidc:~/linux ...

  3. 026_如何在MAC下输入主要国家货币符号?

    由于出国旅游啥的经常会记录一些东西,不避免的会遇到各种货币符号 一. 人民币: ¥(sogo输入法切换到中文模式,然后"shift键 + 4"即可) 美元: $(sogo输入法切换 ...

  4. python初级(302) 1 环境搭建及简单使用

    一.安装anaconda(python环境) 1 地址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A 选择Ana ...

  5. PID:我应该何时计算积分项?

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  6. 关于LPC MUD的关键字及其它重要术语

    关于LPMUD的关键字及其它重要术语 前面的内容中对LPC语言和 lpmud 做了介绍,也完成了学习开发的准备工作,为了更好的学习,这里先对基本术语做一个说明. 关键字(Keywords):LPC语言 ...

  7. [LeetCode] 258. Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  8. kubernetes-subpath用法(把文件挂载在已存在的目录下,不覆盖原目录)

    以ngxin的配置文件为例子: nginx-deployment.yaml : apiVersion: v1 kind: ConfigMap metadata: name: nginx-cm data ...

  9. sql 查找所有员工信息(俩表连接查询)

    题目描述 查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工CREATE TABLE `dept_emp` (`emp_no` in ...

  10. 基于Snappy实现数据压缩和解压

    Snappy是谷歌开源的一个用来压缩和解压的开发包.相较其他压缩算法速率有明显的优势,官方文档显示在64位 i7处理器上,每秒可达200~500MB的压缩速度,不禁感叹大厂的算法就是厉害. 开源项目地 ...