layer弹出图片大小自适应

封装函数

/*弹出图片*/
function previewImg(url) {
    var imgHtml = "<img src='" + url + "' style='width:100%; max-width:100%;'/>";
    // 建立对象
    var img = new Image();
    img.src = url;
    layer.open({
        type: 1,
        shade: 0.5,
        area: [img.width + 'px', img.height + 'px'],
        shadeClose: true,
        scrollbar: false,
        title: false, //显示标题
        content: imgHtml, //捕获的元素,注意:最好该指定的元素要存放在body最外层,不然可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上,包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}

要点

// 建立对象
    var img = new Image();
    img.src = url;
     area: [img.width + 'px', img.height + 'px'],

Done!javascript

相关文章
相关标签/搜索