小卡片左右滑动的实现

背景

项目需求,要实现卡片左右滑动的功能,相似这样:css

图片描述

在实现过程当中遇到了以下问题:ios

  1. 卡片角标实现
  2. 边距问题
  3. 安卓手机适配问题
  4. 翻页问题

角标实现

角标及文字能够采用绝对定位和css3的rotate来实现,注意点是在父元素上要overflow:hiddencss3

.recomm-item-sup{
      position: absolute;
      /*background-color: aquamarine;*/
      color: #fff;
      font-size: 9px;
      height: 34px;
      width: 46px;
      line-height: 58px;
      left: 45px;
      top: -12px;
      -webkit-transform: rotate(45deg);
      transform: rotate(45deg);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

边距问题

这一列卡片初始化时距离手机左边是有一段距离的,可是总体能够滑动到手机最左侧,这一点卡了一点时间,其实就是给第一个卡片设一个margin-left而已;最后一个卡片距离手机最右侧也有边距,但这时候设置margin-right是无效的,此时最右测的卡片会紧贴着屏幕边缘,个人解决办法是在卡片的右侧再写一项卡片,设置这个卡片的宽度是1px,内容为空,这时刚刚的margin-right就有效了。web

<ul class="recom-ul">
        <li class="recomm-item" v-for="(item,index) in slaveButtonListObj.slaveButtonList" v-if="item.isVisible ==1" v-on:click="btnEvent(index)">
          <!--副卡-->
          <div class="recomm-item-con" v-if="item.isHot == 0">
            <img v-if="item.tipIcon && item.title" class="recomm-tip-icon" :src="item.tipIcon" alt="">
            <div v-if="item.tipIcon && item.title" class="recomm-item-sup">{{item.title}}</div>
            <img class="rec-img" :src="item.icon">
            <div class="recomm-item-title">{{item.text}}</div>
          </div>
        </li>
        <li class="recomm-item-visible"></li><!--只为最右测卡片-->
      </ul>

适配问题

适配问题涉及到了两个:
1.卡片的接口数据所有读完并渲染完成后,卡片完美呈现,ios上的滑动也很流畅,可是当滑动的速度很快的时候,安卓上就有问题了,滑动速度过快会致使刚滑出来的卡片变成白板,卡片上的图片和文字都不见了。缘由是浏览器的渲染引擎太慢,解决办法是给ul添加浏览器

transform: translate3d(0,0,0);

这一行代码会触发硬件加速,使用GPU来渲染页面。速度再快也不会有问题了
2.因为采用了overflow:scoll,当卡片多出屏幕时能够滚动呈现,但咱们并不但愿出现滚动条。在安卓上确实没有,但ios上倒是有的。惋惜的是我并无看到有啥属性能够设置滚动条不可见。
后来发现滚动条永远在ul元素的最下面,基本贴在bottom上,因此解决办法是:将ul的高度设高一点,使之超过里面li的高度和滚动条的高度,而后设置ul的父元素overflow:hidden来隐藏掉,OK。
图片描述动画

翻页问题

效果相似轮播图,只是须要在小卡片上进行上下轮播,这里采用的绝对定位+animation来实现的,须要注意的是分段时间的把控,在到达时间的20%的时候,就要到达bottom:0,至时间的50%,一直维持在bottom:0,形成一种静止效果,而后再进行动画滚动。spa

@-webkit-keyframes carouse{
    0%{bottom:-80px;}
    20%{bottom:0}
    50%{bottom:0}
    75%{bottom:80px;}
    100%{bottom:80px;}
  }
.recomm-caro-item{
      position: absolute;
      -webkit-animation:2.5s carouse infinite linear;
      width: 72px;
      height: 80px;
      left: 0;
      right: 0;
      bottom: -80px;
      background: #22ba66;
      border-radius: 5px;
    }
相关文章
相关标签/搜索