vue 圖片橫向無限滾動 scoll

不過於依賴CSS主要核心代碼都在JS中完成

	//獲取ULDOM
	let ul = document.getElementById('ul')
	//UL調用動畫
	ul.style=`animation:scoll ${this.appData.FullImageUrls.length*5}s linear 0s infinite;`
	//獲取ULDOM元素寬度
	let width = window.getComputedStyle(ul).width
	width = width.slice(0,width.length-2)
	width = Number(width/2).toFixed(0)
	//獲取樣式表
	let styleSheets = document.styleSheets
	let domstyle = undefined
	//循環樣式表
	for (let i = 0; i <styleSheets.length ; i++) {
	  let cssStyle = styleSheets[i]
	  //vue打包後樣式表都會放在一個文件中 這裏是爲了找到vue調用的那個樣式表
	  try {
	    if(cssStyle.cssRules.length>1){
	      domstyle=cssStyle
	      break
	    }
	  }catch (e) {
	    continue
	  }
	}
	//給樣式表添加一個動畫樣式
	domstyle.insertRule('@keyframes scoll{from{ left:0;}to{left: -'+width+'px}}',domstyle.length)
//普通的HTML根據自己需求編寫
<ul id="ul">
  <li v-for="(img,index) in imgs" :key="index" >
    <img :src="img" mode="aspectFill" height="100%">
  </li>
  <li v-for="(img,index) in imgs" :key="'c'+index" >
    <img :src="img" mode="aspectFill" height="100%">
  </li>
</ul>
//普通的CSS根據自己需求編寫
  #container{
    width:100%;
    height:250px;
    overflow: hidden;
    position: relative;
    ul{
      list-style: none;
      left:0;
      top:0;
      /*width: 100%;*/
      display: flex;
      flex-shrink:0;
      position: absolute;
      height: 100%;
      li{
        height: 100%;
        float:left;
        margin-right:20px;
      }
    }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章