在前一段時間里使用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是一樣的。
本文為原創轉載請加上本文鏈接,謝謝!