0
点赞
收藏
分享

微信扫一扫

vue 手写手动轮播 且图片宽度不一样

目标践行者 1天前 阅读 2

vue 手写手动轮播 且图片宽度不一样
轮播图样式

  <div class="case-imgs" v-if="length !== 0">

          <div :class="[length === 1 ? 'big' : 'small', 'imgs-wrapper']">

            <img class="case-img" v-for="(m, n) in activeParam.imgs" :key="n" :src="m" :alt="activeParam.name || ''"
              @click="handleImg(n)">

          </div>

          <div class="imgs-btn-wrapper" v-show="length !== 1">

            <div class="left img-btn" :class="{ 'disabled': activeImgIndex === 1 }" @click="handleImgLeft()">

              <img class="btn" src="/img/page/btn.png" alt="" />

              <img class="active" src="/img/page/btn_active.png" alt="" />

            </div>

            <div class="right img-btn" :class="{ 'disabled': activeImgIndex === length }" @click="handleImgRight()">

              <img class="btn" src="/img/page/btn.png" alt="" />

              <img class="active" src="/img/page/btn_active.png" alt="" />

            </div>

          </div>

        </div>
<style scoped>
/* 电脑端 */
.case-wrapper {
  width: 100%;
  margin-top: 20px;
  display: flex;
}

.case-list-line {
  border-right: 2px solid rgba(0, 82, 217, 0.05);
  height: 100%;
  position: absolute;
  top: 0;
  right: 0;
}

/* 左侧菜单 */
.case-menu-list {
  width: 160px;
  overflow: auto;
  text-align: left;
  font-size: 16px;
  font-family: Source Han Sans CN-Regula;
  font-weight: 400;
  color: #333A4A;
  transition: all 0.4s;
  /* border-right: 2px solid rgba(0,82,217,0.05); */
  text-align: center;
  position: relative;
}

.menu-item {
  width: 100%;
  /* padding: 10px 0; */
  height: 60px;
  line-height: 60px;
  cursor: pointer;
}

.menu-item.active {
  color: #0052d9;
  background: rgba(0, 82, 217, 0.03);
  border-right: 4px solid #0052d9;
}

.menu-item:hover {
  color: #2A62FF;
}

/* 右侧内容 */
.case-content {
  width: calc(100% - 150px);
  height: 100%;
  margin-left: 50px;
  overflow: hidden;
}

.case-name {
  width: 100%;
  height: 47px;
  text-align: left;
  font-size: 20px;
  font-family: SourceHanSansSC-Medium;
  font-weight: 500;
  color: #333A4A;
  /* border-bottom: 2px solid #DCDCDC; */
}

.case-imgs {
  margin-top: 0px;
}

.imgs-wrapper {
  width: 100%;
  height: 406px;
  display: flex;
  transition: 0.3s;
}

.imgs-wrapper>img {
  margin-right: 30px;
  border-radius: 3px 3px 3px 3px;
  cursor: pointer;
}

.imgs-wrapper.big>img {
  /* width: calc(100% - 230px); */
  height: 100%;
}

.imgs-wrapper.small>img {
  /* width: calc(100% - 230px); */
  height: 100%;
}

/* 右侧按钮 */
.imgs-btn-wrapper {
  text-align: left;
  margin-top: 20px;
  display: flex;
}

.img-btn {
  position: relative;
  cursor: pointer;
  margin-right: 12px;
}

.img-btn.disabled {
  cursor: not-allowed;
}

.img-btn.disabled:hover>.active {
  opacity: 0;
}

.img-btn>.active {
  position: absolute;
  left: 0px;
  top: 0px;
  opacity: 0;
  transition: all 0.4s;
}

.img-btn:hover>.active {
  opacity: 1;
}

.left>.active {
  transform: rotateY(0.5turn);
}

.right>.btn {
  transform: rotateY(0.5turn);
}

.case-text {
  max-height: 120px;
  overflow: auto;
  text-align: left;
  font-size: 15px;
  font-family: Source Han Sans CN-Regular;
  font-weight: 400;
  color: #333A4A;
  margin-top: 20px;
  margin-bottom: 20px;
}

::-webkit-scrollbar {
  width: 2px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  border-radius: 5px;
  background-color: #2A62FF;
}

::-webkit-scrollbar-track {
  background-color: #DCDCDC;
}

/* 手机端 */
.phone-case {
  width: 100%;
}

.phone-caseimg {
  max-width: 100%;
  margin-top: 20px;
}

.case-name-phone {
  width: 100%;
  height: 47px;
  text-align: left;
  font-size: 20px;
  font-family: SourceHanSansSC-Medium;
  font-weight: 500;
  color: #333A4A;
}
</style>

下面的实现左右切换的方法

  methods: {
    handleMenu(e, i, param) {
      this.activeIndex = i
      this.activeParam = param
      this.activeImgIndex = 1
      // 动态修改左侧菜单的高度
      if(this.screenWidth >560){
        setTimeout(() => {
        let height = document.querySelector('.case-content').offsetHeight
        document.querySelector('.case-menu-list').style.height = height + 'px'
        let ele = document.querySelector('.imgs-wrapper')
        ele.style.transform = 'translateX(0px)'
        this.length = this.activeParam.imgs.length
      }, 300)
      }

    },
    handleImgLeft() {
      if (this.activeImgIndex !== 1) {
        this.activeImgIndex--
        this.imgMove(false)
      }
    },
    handleImgRight() {
      if (this.activeImgIndex !== this.activeParam.imgs.length) {
        this.activeImgIndex++
        this.imgMove(true)
      }
    },
    handleImg(index) {
      if (index + 1 !== this.activeImgIndex) {
        this.activeImgIndex = index + 1
        this.imgMove()
      }
    },
    imgMove(flag) {
      let width = document.querySelectorAll('.case-img')

      if (flag) {
        width = width[this.activeImgIndex - 2].offsetWidth
        this.moveLeftWidth += (width + 30)
      } else {
        width = width[this.activeImgIndex - 1].offsetWidth
        this.moveLeftWidth -= (width + 30)
      }
      let ele = document.querySelector('.imgs-wrapper')
      // let moveWidth = (width + 90) * (this.activeImgIndex - 1)
      // ele.style.transform = 'translateX(-' + moveWidth + 'px)'
      ele.style.transform = 'translateX(-' + this.moveLeftWidth + 'px)'
    },
    initNum(param) {
      if (param < 10) {
        param = '0' + param;
      }
      return param
    }
    // initNum(param) {
    //   let arr = {
    //     0: '零',
    //     1: '一',
    //     2: '二',
    //     3: '三',
    //     4: '四',
    //     5: '五',
    //     6: '六',
    //     7: '七',
    //     8: '八',
    //     9: '九',
    //     10: '十',
    //   }
    //   let capNum = ''
    //   if (param <= 10) {
    //     capNum = arr[param]
    //   } else if (param < 20) {
    //     let num = Array.from(param.toString())
    //     capNum = '十' + arr[num[1]]
    //   } else if (param === 20) {
    //     capNum = '二十'
    //   } else if (param === 30) {
    //     capNum = '三十'
    //   } else if (param === 40) {
    //     capNum = '四十'
    //   } else if (param === 50) {
    //     capNum = '五十'
    //   } else if (param === 60) {
    //     capNum = '六十'
    //   } else if (param === 70) {
    //     capNum = '七十'
    //   } else if (param === 80) {
    //     capNum = '八十'
    //   } else {
    //     let num = Array.from(param.toString())
    //     capNum = arr[num[0]] + '十' + arr[num[1]]
    //   }
    //   return capNum
    // }
  }
举报

相关推荐

0 条评论