小編給大家分享一下微信小程序中組件marquee有什么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
網站建設哪家好,找創新互聯建站!專注于網頁設計、網站建設、微信開發、小程序設計、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了勃利免費建站歡迎大家使用!
微信小程序組件 marquee實例詳解
1. marquee標簽
html是有marquee標簽的,可以實現跑馬燈效果,但小程序沒有,所以要實現。這里考慮使用css3的animation實現。
html的marquee是這樣使用的。
<marquee direction="left" behavior="scroll" scrollamount="1" scrolldelay="0" loop="-1" width="200" height="50" bgcolor="#0099FF" hspace="10" vspace="10"> hello world </marquee>
2. wxml
<view class="marquee_container" > <view class="marquee_text">{{marquee.text}}</view> </view>
傳入wxml的是個json對象
marquee:{ width:12, text:'hello world' }
而那個奇怪的--marqueeWidth是給@keyframes傳的變量。內聯設置變量,css文件中也可以獲取到該變量。
3. wxss
@keyframes around { from { margin-left: 100%; } to { margin-left: var(--marqueeWidth--);// var接受傳入的變量 } } .marquee_container{ background-color: #0099FF; height: 1.2em; position: relative; width: 100%; } .marquee_container:hover{ animation-play-state: paused; // 不起作用 } .marquee_text{ display: inline-block; white-space: nowrap; animation-name: around; animation-duration: 5s; animation-iteration-count: infinite; animation-timing-function:linear; }
4. js
export default { getWidth:(str)=>{ return [].reduce.call(str, (pre, cur, index, arr) => { if (str.charCodeAt(index) > 255) {// charCode大于255是漢字 pre++; } else { pre += 0.5; } return pre; }, 0); }, getDuration:(str)=>{// 保留,根據文字長度設置時間 return this.getWidth()/10; } }
以上是組件的封裝。
5. 使用
// wxml <include src="../component/marquee/marquee.wxml" /> // wxss @import "../component/marquee/marquee.wxss"; // js import marquee from '../component/marquee/marquee.js'; var options = Object.assign(marquee, { data: { motto: 'Hello World', userInfo: {}, marquee: { text: '你好,中國!hello,world!' } }, onLoad: function () { // ... const str = this.data.marquee.text; const width = this.getWidth(str); console.log('width',width); this.setData({ [`${'marquee'}.width`]: width }); } }); Page(options);
看完了這篇文章,相信你對“微信小程序中組件marquee有什么用”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
當前標題:微信小程序中組件marquee有什么用
鏈接分享:http://m.2m8n56k.cn/article46/pchjhg.html
成都網站建設公司_創新互聯,為您提供網站策劃、全網營銷推廣、微信公眾號、網站建設、網站設計公司、電子商務
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯