在移动端开发时使用photoswipe插件报错 Uncaught TypeError: Cannot read property 'x' of undefined
在前一段时间里使用photoswipe这个插件时总是会报错Cannot read property 'x' of undefined 一直不知道是什么原因,自从分析了一下这个插件以后发现在使用者个强大的插件时插入的图片都需要给添加上图片的尺寸“data-size=670x712”,报错提示的信息就是这个原因,错误中的'x'就是这个添加的data-size中的值。
在发现这个问题以后需要给你的这个代码添加上这个,这样就能够顺利的解决报错。
博主顺便分享出来,为以后遇到这问题的童鞋提供个方便,代码如下:
function data_size(){ $("figure img").each(function () { var imgs = new Image(); imgs.src = $(this).attr("src"); var w = imgs.width * 10, h = imgs.height * 10; $(this).parent("a").attr("data-size", "").attr("data-size", w + "x" + h); }) }
这个代码需要在页面一载入时就执行,html的部分和网友们提供的大部分的demo是一样的。
本文为原创转载请加上本文链接,谢谢!