加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

微信小程序组件 marquee实例详解

发布时间:2020-12-14 20:14:52 所属栏目:资源 来源:网络整理
导读:微信小程序组件 marquee实例详解 1. marquee标签 html是有marquee标签的,可以实现跑马灯效果,但小程序没有,所以要实现。这里考虑使用css3的animation实现。 html的marquee是这样使用的。 2. wxml 传入wxml的是个json对象 而那个奇怪的--marqueeWidth是给@

微信小程序组件 marquee实例详解

1. marquee标签

html是有marquee标签的,可以实现跑马灯效果,但小程序没有,所以要实现。这里考虑使用css3的animation实现。

html的marquee是这样使用的。

2. wxml

传入wxml的是个json对象

而那个奇怪的--marqueeWidth是给@keyframes传的变量。内联设置变量,css文件中也可以获取到该变量。

3. wxss

.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

{ 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. 使用

// 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);

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读