這篇文章給大家分享的是有關(guān)js如何實現(xiàn)圖片加載淡入淡出效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
龍華ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
HTML代碼
首先是圖片標記的寫法:
<img data-src="/path/to/image.jpg" alt="">
需要將圖片的地址放到 data-src 屬性里,而src值填寫默認的一張圖片。
CSS代碼
所有具有data-src屬性的圖片,我們將其初始顯示狀態(tài)為不可見,通過透明度來調(diào)節(jié):
img { opacity: 1; transition: opacity 0.3s; } img[data-src] { opacity: 0; }
這樣寫的作用是什么?等當圖片加載時,你就能看的效果了。
JavaScript代碼
我們最終會將 data-src 屬性去掉,換成src屬性,但這是圖片加載成功后的動作:
[].forEach.call(document.querySelectorAll('img[data-src]'), function(img) { img.setAttribute('src', img.getAttribute('data-src')); img.onload = function() { img.removeAttribute('data-src'); }; });
相比起其它各種的圖片延遲加載技術(shù),這種方法非常的簡單,它幾乎不要求其它任何條件,可以用在任何地方,使用起來非常靈活。
當然,簡單有簡單的好壞,也會因為簡單而不足。它不具有圖片圖片滾動到可視窗口內(nèi)再加載的功能。最終使用哪種技術(shù),還是要看場景而定。
下面是lazyload.js
var lazyLoad = { init: function() { var that = this; that.onerrorImgUrl = "data-error"; //圖片加載失敗用什么圖片替換 that.srcStore = "data-src"; //圖片真實地址存放的自定義屬性 that.class = "lazy-img"; //惰性加載的圖片需要添加的class that.sensitivity = 50; //該值越小,惰性越強(加載越少) minScroll = 5, slowScrollTime = 200; document.addEventListener("scroll", function() { that.changeimg(); }); setTimeout(function() { that.trigger(); }, 100); }, scanImage: function() { var that = this; var imgList = []; var allimg = [].slice.call(document.querySelectorAll('img.' + that.class + '')); allimg.forEach(function(ele) { if (!that.isLoadedImageCompleted(ele)) { imgList.push(ele); } }); that.imglistArr = imgList; }, isLoadedImageCompleted: function(ele) { return (ele.getAttribute('data-loaded') == '1') }, trigger: function() { var that = this; eventType = that.isPhone && "touchend" || "scroll"; that.fireEvent(document, eventType); //$(window).trigger(eventType); }, fireEvent: function(element, event) { // 其他標準瀏覽器使用dispatchEvent方法 var evt = document.createEvent('HTMLEvents'); // initEvent接受3個參數(shù): // 事件類型,是否冒泡,是否阻止瀏覽器的默認行為 evt.initEvent(event, true, true); return !element.dispatchEvent(evt); }, changeimg: function() { function loadYesOrno(img) { var windowPageYOffset = window.pageYOffset, windowPageYOffsetAddHeight = windowPageYOffset + window.innerHeight, imgOffsetTop = img.getBoundingClientRect().top + window.pageYOffset; return imgOffsetTop >= windowPageYOffset && imgOffsetTop - that.sensitivity <= windowPageYOffsetAddHeight; } function loadImg(img, index) { var imgUrl = img.getAttribute(that.srcStore); img.setAttribute("src", imgUrl); img.onload || (img.onload = function() { img.classList.remove(that.class); img.setAttribute('data-loaded', 1) img.removeAttribute('data-src'); //$(this).removeClass(that.class).getAttribute('data-loaded',1), that.imglistArr[index] = null; img.onerror = img.onload = null; }, img.onerror = function() { img.src = img.getAttribute(that.onerrorImgUrl); img.classList.remove(that.class); img.classList.add("lazy-err"); img.setAttribute('data-loaded', 0); //$(this).removeClass(that.class).getAttribute('data-loaded',0), that.imglistArr[index] = null, img.onerror = img.onload = null }); var newImgStack = []; that.imglistArr.forEach(function(ele) { //img標簽可見并且加載未完成 if (!that.isLoadedImageCompleted(ele)) { newImgStack.push(ele); } }); that.imglistArr = newImgStack; } var that = this; that.scanImage(); that.imglistArr.forEach(function(val, index) { if (!val) return; var img = val; if (!loadYesOrno(img) || that.isLoadedImageCompleted(img)) return; if (!img.getAttribute(that.srcStore)) return; loadImg(img, index); }) } };
感謝各位的閱讀!關(guān)于“js如何實現(xiàn)圖片加載淡入淡出效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章標題:js如何實現(xiàn)圖片加載淡入淡出效果
轉(zhuǎn)載注明:http://aaarwkj.com/article18/pjdidp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、、品牌網(wǎng)站制作、品牌網(wǎng)站設計、營銷型網(wǎng)站建設、手機網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)