微信小程序组件 marquee实例详解
2019/6/27 20:52:00
本文主要是介绍微信小程序组件 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实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist