当前位置: 首页 > Web前端 > CSS

uniapp操作dom,改变css样式

时间:2023-03-30 14:14:43 CSS

在uniapp中,我想给元素添加一些动态的css样式,比如transform,uni.createSelectorQuery().select(".sticke").boundingClientRect((res)=>{uni.createSelectorQuery().select(".sticke").style.transform}).exec()上面的代码根本无法给元素添加transform,所以通过声明的css变量,在swiper组件中添加缩略图显示,点击某一项,显示对应的图片1.重点在数据中注册属性transStyle2.设置轮播变化事件transStylegetDetail(e,index){this.currentIndex=indexthis.detail=Object.assign(this.detail,e)this.scrollLeft=index*50this.transStyle='translate('+index*-100+'%,0px)'},3.点击缩略图,关闭自动播放,延迟执行move(e,index){这。autoplay=falsethis.currentIndex=indexthis.detail=Object.assign(this.detail,e)this.scrollLeft=index*50this.transStyle='translate('+index*-100+'%,0px)'setTimeout(()=>{this.autoplay=true},5000)}4。找到要更改的元素并设置css变量#swiper{.uni-swiper-slide-frame{transform:var(--transStyle)!important;}}