微信小程序 实现跑马灯(文字+图片)
參考鏈接:
(1)詳解微信小程序實現跑馬燈效果(附完整代碼)
https://www.jb51.net/article/160412.htm
(2)初學者微信小程序—實現圖片輪播效果
https://blog.csdn.net/qq_42594368/article/details/88659743
(3)微信小程序-使用swiper和css實現卡牌左右滑動切換、翻牌效果
https://blog.csdn.net/vinos_toby/article/details/90669406
(4)微信小程序實現卡片左右滑動效果的示例代碼
https://www.jb51.net/article/160569.htm
一、文字跑馬燈
在微信小程序里實現跑馬燈效果,類似滾動字幕或者滾動廣告之類的,使用簡單的CSS樣式控制,沒用到JS,效果如下圖:
wxml文件
wxss文件
/*首頁跑馬燈效果*/ @keyframes around {from {margin-left: 100%;}to {/* var接受傳入的變量 */margin-left: var(--marqueeWidth--);}} .marquee_container{background-color: #fe4655;height: 50rpx;line-height: 44rpx;position: relative;width: 100%;margin-top:0rpx; } .marquee_container:hover{/* 不起作用 */animation-play-state: paused; } .marquee_text{color:#fff;font-size: 28rpx;display: inline-block;white-space: nowrap;animation-name: around;animation-duration: 10s; /*過渡時間*/animation-iteration-count: infinite;animation-timing-function:linear; } /*首頁跑馬燈效果*/二、圖片輪播圖/圖片跑馬燈
效果圖
wxml文件
注:參數詳解
wxss文件
.lunbo{width:100% } .lunbo image{width:100% }三、卡片左右滑動切換、翻牌效果
效果圖(演示視頻效果見 https://www.duoguyu.com/smart/27.html)
滑塊功能:使用了微信小程序組件-滑塊視圖容器 Swiper (查看官方文檔)。
翻牌旋轉效果:使用了Css3的一些屬性:perspective、backface-visibility、transform等(參考鏈接:《Css3實現翻牌效果》DEMO源碼 https://www.jq22.com/webqd4252?v=duoguyu.com)
perspective:3000rpx; /*perspective 屬性定義 3D 元素距視圖的距離,以像素計。該屬性允許您改變 3D 元素查看 3D 元素的視圖。 當為元素定義 perspective 屬性時,其子元素會獲得透視效果,而不是元素本身。*/ backface-visibility:hidden; /*背對屏幕時隱藏*/ transform-style: preserve-3d; /*子元素將保留其3D位置。*/ transform:rotateY(180deg); /*定義沿著Y軸的3D旋轉。*/源碼Demo下載地址:https://www.duoguyu.com/smart/27.html
四、卡片左右滑動
效果圖
- 思路
從上面的效果圖來看,基本的需求包括:
左右滑動到一定的距離,就向相應的方向移動一個卡片的位置。
卡片滑動的時候有一定的加速度。
如果滑動距離太小,則依舊停留在當前卡片,而且有一個回彈的效果。
看到這樣的需求,不熟悉小程序的同學,可能感覺有點麻煩。首先需要計算卡片的位置,然后再設置滾動條的位置,使其滾動到指定的位置,而且在滾動的過程中,加上一點加速度…
然而,當你查看了小程序的開發文檔之后,就會發現小程序已經幫我們提前寫好了,我們只要做相關的設置就行。
- 實現
滾動視圖:左右滑動,其實就是水平方向上的滾動。小程序給我們提供了scroll-view組件,我們可以通過設置scroll-x屬性使其橫向滾動。 - 關鍵屬性
在scroll-view組件屬性列表中,我們發現了兩個關鍵的屬性:
| scroll-into-view | string | 值應為某子元素id(id不能以數字開頭)。設置哪個方向可滾動,則在哪個方向滾動到該元素 |
| scroll-with-animation | boolean | 在設置滾動條位置時使用動畫過渡 |
有了以上這兩個屬性,我們就很好辦事了。只要讓每個卡片獨占一頁,同時設置元素的ID,就可以很簡單的實現翻頁效果了。
- 左滑右滑判斷
這里,我們通過觸摸的開始位置和結束位置來決定滑動方向。
微信小程序給我們提供了touchstart以及touchend事件,我們可以通過判斷開始和結束的時候的橫坐標來判斷方向。
wxml文件
<scroll-view class="scroll-box" scroll-x scroll-with-animationscroll-into-view="{{toView}}"bindtouchstart="touchStart"bindtouchend="touchEnd"><view wx:for="{{list}}" wx:key="{{item}}" class="card-box" id="card_{{index}}"><view class="card"><text>{{item}}</text></view></view> </scroll-view>wxss文件
page{overflow: hidden;background: #0D1740; } .scroll-box{white-space: nowrap;height: 105vh; }.card-box{display: inline-block; }.card{display: flex;justify-content: center;align-items: center;box-sizing: border-box;height: 80vh;width: 80vw;margin: 5vh 10vw;font-size: 40px;background: #F8F2DC;border-radius: 4px; }js文件
const DEFAULT_PAGE = 0;Page({startPageX: 0,currentView: DEFAULT_PAGE,data: {toView: `card_${DEFAULT_PAGE}`,list: ['Javascript', 'Typescript', 'Java', 'PHP', 'Go']},touchStart(e) {this.startPageX = e.changedTouches[0].pageX;},touchEnd(e) {const moveX = e.changedTouches[0].pageX - this.startPageX;const maxPage = this.data.list.length - 1;if (Math.abs(moveX) >= 150){if (moveX > 0) {this.currentView = this.currentView !== 0 ? this.currentView - 1 : 0;} else {this.currentView = this.currentView !== maxPage ? this.currentView + 1 : maxPage;}}this.setData({toView: `card_${this.currentView}`});} })json文件
{"navigationBarTitleText": "卡片滑動","backgroundColor": "#0D1740","navigationBarBackgroundColor": "#0D1740","navigationBarTextStyle": "white" }總結
以上是生活随笔為你收集整理的微信小程序 实现跑马灯(文字+图片)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机组成原理题库带答案详解,计算机组成
- 下一篇: AutowireCapableBeanF