微信小程序組件 marquee實(shí)例詳解
微信小程序組件 marquee實(shí)例詳解
1. marquee標(biāo)簽
html是有marquee標(biāo)簽的,可以實(shí)現(xiàn)跑馬燈效果,但小程序沒有,所以要實(shí)現(xiàn)。這里考慮使用css3的animation實(shí)現(xiàn)。
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" style="--marqueeWidth--:{{-marquee.width}}em"> <view class="marquee_text">{{marquee.text}}</view> </view>
傳入wxml的是個(gè)json對(duì)象
marquee:{ width:12, text:'hello world' }
而那個(gè)奇怪的--marqueeWidth是給@keyframes傳的變量。內(nèi)聯(lián)設(shè)置變量,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)=>{// 保留,根據(jù)文字長(zhǎng)度設(shè)置時(shí)間 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);
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
jQuery進(jìn)階實(shí)踐之利用最優(yōu)雅的方式如何寫ajax請(qǐng)求
ajax請(qǐng)求相信對(duì)大家來說都不陌生,下面這篇文章主要介紹了jQuery進(jìn)階實(shí)踐之利用最優(yōu)雅的方式如何寫ajax請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12jquery實(shí)現(xiàn)動(dòng)態(tài)改變css樣式的方法分析
這篇文章主要介紹了jquery實(shí)現(xiàn)動(dòng)態(tài)改變css樣式的方法,結(jié)合實(shí)例形式分析了jQuery動(dòng)態(tài)操作css樣式的設(shè)置、獲取及應(yīng)用等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05jquery 動(dòng)態(tài)增加刪除行的簡(jiǎn)單實(shí)例(推薦)
下面小編就為大家?guī)硪黄猨query 動(dòng)態(tài)增加刪除行的簡(jiǎn)單實(shí)例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10jQuery+.net實(shí)現(xiàn)瀏覽更多內(nèi)容(改編php版本)
改編自php版本這里記錄.net 下的實(shí)現(xiàn);首先創(chuàng)建數(shù)據(jù)庫表test,并插入一些測(cè)試數(shù)據(jù)接下來建立一個(gè)html文件,感興趣的朋友可以參考下哈,希望您可以幫助到你2013-03-03自己動(dòng)手制作jquery插件之自動(dòng)添加刪除行功能介紹
這個(gè)插件的上篇訪問量很不好,幾乎是我寫的文章里最少的點(diǎn)擊量的了,不知道是不是因?yàn)榇蠹覍?duì)我說的這個(gè)插件不感興趣還是說我寫的東西技術(shù)含量太差了,呵,那我只能孤芳自賞了2011-10-10jquery 注意事項(xiàng)與常用語法小結(jié)
jquery 注意事項(xiàng)與常用語法小結(jié),學(xué)習(xí)jquery的朋友最好收藏下。2010-06-06JQuery 無廢話系列教程(二) jquery實(shí)戰(zhàn)篇上
就進(jìn)入實(shí)戰(zhàn)?是不是太快了?我還不知道怎么用JQuery!! ...2009-06-06input輸入框內(nèi)容實(shí)時(shí)監(jiān)測(cè)(附代碼)
這篇文章主要介紹了如何實(shí)時(shí)監(jiān)測(cè)input輸入框內(nèi)容,具體操作步驟大家可查看下文詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08jQuery實(shí)現(xiàn)的表頭固定效果實(shí)例【附完整demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的表頭固定效果,結(jié)合完整實(shí)例形式分析了jQuery基于插件實(shí)現(xiàn)的表頭固定功能與用法,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-08-08