layui 实现点击图片进行预览

  1. 首先写一下预览方法
	function previewImg(obj) {
        var img = new Image();  
        img.src = obj.src;
        var imgHtml = "<img src='" + obj.src + "' />";  
       	//弹出层
        layer.open({  
            type: 1,  
            shade: true,  
            title: false, //不显示标题  
            area:['auto','auto'],
            content: imgHtml, //捕获的元素,注意:最好该指定的元素要存放在body最外层,否则可能被其它的相对元素所影响  
            cancel: function () {  
                
            }  
        }); 
    }
  1. 然后获取页面中的img对象并添加点击事件
	//图片预览事件
	  $("#post-content figure img").each(function() {
        $(this).click(function() {
        	previewImg(this);
        })
  		  });
发布了22 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_19408473/article/details/105184653
今日推荐