头闻号

江苏金陵特种涂料有限公司

特种涂料|防腐涂料|涂料、油漆

首页 > 新闻中心 > 科技常识:使用backgroundImage解决图片轮播切换
科技常识:使用backgroundImage解决图片轮播切换
发布时间:2023-02-01 10:29:40        浏览次数:6        返回列表

今天小编跟大家讲解下有关使用backgroundImage解决图片轮播切换 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关使用backgroundImage解决图片轮播切换 的相关资料,希望小伙伴们看了有所帮助。

单dom节点实现轮播

利用backgroundImage可以添加多张图片,以及位置偏移实现轮播效果

创建一个div;并用backgroundImage给div附图片利用backgroundPosition调节位置利用css3 transition调节过渡即可替代简单的图片切换

代码 function playImage(src) { if (animaitionFinshed) return; if (!_imageEl) { _imageEl = document.createElement('div') _imageEl.className = `swiper_container`; _imageEl.style.backgroundImage = `url(${src.url})`; _imageEl.setAttribute("data-img", src.url); elContainer.appendChild(_imageEl); } else { animaitionFinshed = true; let width = elContainer.clientWidth, height = elContainer.clientHeight; let preImage = _imageEl.getAttribute("data-img"); _imageEl.style.backgroundImage = `url(${preImage}),url(${src.url}) `; _imageEl.style.backgroundPositionX = `center,${width + 20}px`; setTimeout(() => { _imageEl.style.transition ="all 0.8s ease"; _imageEl.style.backgroundPositionX = `-${width + 20}px,center`; }, 0); setTimeout(() => { _imageEl.style.transition ="none"; _imageEl.style.backgroundImage = `url(${src.url}) `; _imageEl.style.backgroundPositionX = `center`; _imageEl.setAttribute("data-img", src.url) animaitionFinshed = false; }, 800) } }

来源:爱蒂网