scroll-view内部实现图片放大

今天遇到一个需求是在一个scroll-view里面很多item,点击其中一个放大,刚开始想着用width和height控制

.theme-list {
  height: 230rpx;
  /* background-color: lightblue; */
  white-space: nowrap;
  display: flex;
}

.theme-item {
  height: 100%;
  width: 300rpx;
  text-align: center;
  font-size: 24rpx;
  margin-right: 10rpx;
  display: inline-block;
}

.theme-title{
  height: 36rpx;
}

.theme-item:last-child {
  margin-right: 0;
}

.img-box {
  width: 300rpx;
  height: 180rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.img-box > .image{
  height: 90%;
  width: 90%;
  transition: all, 0.5s;
}

.title-active{
  color: pink;
  font-size: 28rpx;
  transition: all 0.5s;
}
/*这里使用width和height控制大小*/
.img-box > .active{
  width: 100%;
  heigth: 100%;
}
<!--components/c2b-show-box/c2b-show-box.wxml-->
<view class="scroll-box">
  <scroll-view class="theme-list" scroll-x="true" scroll-into-view="{{toView}}"
  scroll-with-animation="true"
  >
    <view class="theme-item" 
    id="item{{index}}" wx:for="{{themes}}" data-index="{{index}}" bindtap="viewTheme" >
      <view class="theme-title {{index == themeSelected ? 'title-active' : ''}}">{{item.title}}</view>
      <view class="img-box">
        <image class="image {{index == themeSelected ? 'active' : ''}}"src="{{item.src}}"></image>a
      </view>
    </view>
  </scroll-view>
</view>
// components/c2b-show-box/c2b-show-box.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    themes:{
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    toView: '',
    themeSelected: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    viewTheme: function(e){
      console.log(e)
      let ind = e.currentTarget.dataset.index
      this.setData({
        themeSelected: ind,
        toView: 'item' + (ind > 1 ? ind - 1 : 0)      
      }) 
    }
  }
})

结果发现在点击时其它元素会发生抖动。ps:git就不闹了,想知道情况的小伙伴可以自己试下。
然后看了看老大写的代码,发现是用scale改变大小。抄过来发现其它元素真的不抖动了。以后等比例放大的话还是用scale吧。

/* components/c2b-show-box/c2b-show-box.wxss */
.theme-list {
  height: 230rpx;
  /* background-color: lightblue; */
  white-space: nowrap;
  display: flex;
}

.theme-item {
  height: 100%;
  width: 300rpx;
  text-align: center;
  font-size: 24rpx;
  margin-right: 10rpx;
  display: inline-block;
}

.theme-title{
  height: 36rpx;
}

.theme-item:last-child {
  margin-right: 0;
}

.img-box {
  width: 300rpx;
  height: 180rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.img-box > .image{
  height: 162rpx;
  width: 270rpx;
  transition: all, 0.5s;
}

.title-active{
  color: pink;
  font-size: 28rpx;
  transition: all 0.5s;
}

.img-box > .active{
  transform: scale(1.1);
}

更新于2021-4-2:今天突然面试突然问起这个问题,又结合最近学到的知识,感觉这个内部放大抖动的问题应该是改变几何尺寸引起重绘和重流了,用tansform的话就不会引起重绘了重流了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容