微信小程序實現輪播圖標題跑馬燈
更新時間:2022年06月24日 14:19:04 作者:Zmikoo成長之路
這篇文章主要為大家詳細介紹了微信小程序實現輪播圖標題跑馬燈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序實現輪播圖標題跑馬燈的具體代碼,供大家參考,具體內容如下
微信小程序做輪播圖,輪播圖下的標題如果不長不需要跑馬燈效果,過長的無法顯示全的則添加跑馬燈效果
<swiper class="swiper" current="0" bindchange="onSlideChange"> ? ? <swiper-item wx:for='{{carouselImgArr}}' wx:key='index'> ? ? ? <image? ? ? ? ? ? src='{{item.image}}'? ? ? ? ? ? mode='heightFix' ? ? ? ? ? class="swiper-item-img"> ? ? ? </image> ? ? ? <view class="swiper-item-tit" wx:if='{{item.title}}'> ? ? ? ? <view class="swiper-tit-inner {{(currImgIndex - 1) == index ? 'active' : ''}}"? ? ? ? ? ? ? ? style='transform:translate({{ ((currImgIndex - 1) == index ? carouselTitLeft : 0) + "px" }})'> ? ? ? ? ? {{item.title}} ? ? ? ? </view> ? ? ? </view> ? ? </swiper-item> </swiper>
.swiper{ ? position: relative; ? height: 430rpx; ? padding: 0px; ? margin: 0px; } .swiper image{ ? height: 430rpx; ? position: absolute; ? left:50%; ? top:50%; ? transform: translate(-50%,-50%); } .swiper-item-tit{ ? position: absolute; ? bottom: 0rpx; ? left:0rpx; ? z-index: 2; ? height: 80rpx; ? line-height: 80rpx; ? color:#fff; ? width:100%; ? /* overflow: hidden; */ ? /* text-overflow: ellipsis; */ ? background-color: rgba(0,0,0,0.5); } .swiper-item-tit .swiper-tit-inner{ ? text-align: center; ? white-space: nowrap; }?
data: { ? ? carouselImgArr: [{ ?? ??? ?image:'../../image/1.png',title:'標題', ?? ?},{ ?? ??? ?image:'../../image/1.png',title:'標題標題標題標題標題標題標題標題標題', ?? ?},{ ?? ??? ?image:'../../image/1.png',title:'標題', ?? ?} ], ? ? carouselTitleLength:[2,18,2],// 輪播圖標題的文字長度 ? ? carouselTitLeft:0, ? ? currImgIndex: 1, ? ? windowWidth: wx.getSystemInfoSync().windowWidth ? }, onSlideChange(e) { ? ? this.setData({ ? ? ? currImgIndex: e.detail.current + 1, ? ? ? carouselTitLeft: 0 ? ? }); ? ? this.initMarqueen(); ? }, ? initMarqueen() { ? ? clearInterval(marqueenTimer); ? ? var self = this; ? ? var boxWidth,textWidth; ? ? var query = wx.createSelectorQuery(); ? ? // query.select('.swiper-item-tit').fields({size:true},function(res){ ? ? // ? boxWidth = res.width; ? ? // }).exec(); ? ? // query.select('.active').fields({size:true},function(res){ ? ? // ? textWidth = res.width; ? ? // }).exec(); ? ? setTimeout(function(){ ? ? ? let boxWidth = self.data.windowWidth;// 屏幕尺寸寬等于字體box寬,所以這里用屏幕寬替代了query獲取的寬 ? ? ? let scale = boxWidth / 375;// 以屏幕尺寸為375為標準(375下字體寬約為14),根據屏幕尺寸計算單個字體的寬度 ? ? // 不知道為什么用query 獲取的textWidth始終等于boxWidth的寬度,不得已只能使用文字長度乘以文字寬度來計算boxWidth。而 ? ? ? let textWidth = self.data.carouselTitleLength[self.data.currImgIndex - 1] * (14*scale); ? ? ? console.log(scale,boxWidth,textWidth); ? ? ? if (textWidth - 3*scale > boxWidth) {// 減去3*scale防止textWidth只多出來一點點導致文字左右震蕩 ? ? ? ? let stayTime = 1000; ? ? ? ? let endStay = true; ? ? ? ? marqueenTimer = setInterval(function(){ ? ? ? ? ? let currLeft = self.data.carouselTitLeft; ? ? ? ? ? if (stayTime !== 0) { ? ? ? ? ? ? stayTime = stayTime - 30; ? ? ? ? ? ? console.log('stay') ? ? ? ? ? } else if (currLeft > boxWidth - textWidth) { ? ? ? ? ? ? self.setData({ ? ? ? ? ? ? ? carouselTitLeft: currLeft - 2 ? ? ? ? ? ? }); ? ? ? ? ? } else { ? ? ? ? ? ? if (endStay) {// 跑馬燈結尾停留1s; ? ? ? ? ? ? ? endStay = false; ? ? ? ? ? ? ? stayTime = 1200; ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? self.setData({ ? ? ? ? ? ? ? carouselTitLeft: 0 ? ? ? ? ? ? }); ? ? ? ? ? ? stayTime = 1200;// 回到跑馬燈開頭再停留1s; ? ? ? ? ? ? endStay = true; ? ? ? ? ? } ? ? ? ? },100) ? ? ? } ? ? },100); ? },
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Js中FileReader讀取文件內容方法詳解(async/await)
這篇文章主要給大家介紹了關于Js中FileReader讀取文件內容(async/await)的相關資料,FileReader是前端進行文件處理的一個重要的Api,特別是在對圖片的處理上,如果你想知道圖片的處理原理,你就永遠不可能繞過它,需要的朋友可以參考下2023-11-11詳解JavaScript中typeof與instanceof用法
typeof用以獲取一個變量或者表達式的類型而instanceof用于判斷一個變量是否某個對象的實例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-10-10使用getBoundingClientRect方法實現簡潔的sticky組件的方法
本文介紹這種組件的實現思路,并提供一個同時支持將sticky元素固定在頂部或底部的具體實現,由于這種組件在網站中非常常見,所以有必要掌握它的實現方式,以便在有需要的時候基于它的思路寫出功能更多的組件出來2016-03-03