使用backgroundImage解决图片轮播切换

单dom节点实现轮播 行使backgroundImage可以添加多张图片,以及位置偏移实现轮播效果   建立一个div;并用backgroundImage给div附图片 行使backgroundPosition调治位置 行使css3 transition调...

单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)
        }
    }
思源资源网:分类流动

1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入

2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入

3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入

链接: http://www.fly63.com/article/detial/4555

  • 发表于 2021-04-17 15:52
  • 阅读 ( 245 )
  • 分类:互联网

0 条评论

请先 登录 后评论
q2828
q2828

703 篇文章

你可能感兴趣的文章

相关问题