這篇“微信小程序組件marquee如何用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“微信小程序組件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" style="--marqueeWidth--:{{-marquee.width}}em">
<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如何用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。