layer 预览图片自适应居中的问题

 1 function openLayer(imgUrl) {
 2   var imgUrl = imgUrl;
 3   getImageWidth(imgUrl, function(w, h){
 4     w = h > 860? w/h*860: w
 5     h = h > 860? 860: h
 6     parent.layer.open({
 7         type: 1,
 8         title: false,
 9         offset: 'auto',
10         area: [ w+'px', h+'px' ],
11         shadeClose: true,
12         content: '<div><img style="max-width: 100%;max-height: 100%" src="'+imgUrl+'"></div>'
13     });
14   });
15 }
16 
17 // 获取图片真实高度
18 function getImageWidth(url, callback) {
19   var img = new Image();
20   img.src = url;
21   // 如果图片被缓存,则直接返回缓存数据
22   if (img.complete) {
23     callback(img.width, img.height);
24   } else {
25     img.onload = function () {
26       callback(img.width, img.height);
27     }
28   }
29 }
30 
31 $('.pics-container').on('click', '.photo-item img', function() {
32   openLayer(this.src.replace('http://', 'https://'))
33 })

猜你喜欢

转载自www.cnblogs.com/ywenhao/p/12217464.html