JavaScript實(shí)現(xiàn)頁面無縫滾動(dòng)效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)頁面無縫滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
目前我只使用兩種方式,如果還有其他方式,希望推薦一下。
1、js+transform
使用定時(shí)器動(dòng)態(tài)增加大小,再把值賦給 transform,實(shí)現(xiàn)位置偏移,來實(shí)現(xiàn)無縫滾動(dòng)。
html
一定要循環(huán)兩遍數(shù)據(jù),這樣的話,會(huì)出現(xiàn)兩個(gè)一樣的數(shù)據(jù),在一個(gè)數(shù)據(jù)消失后,不會(huì)使頁面空白,而這時(shí)transform歸0,有從頭開始,因?yàn)閮蓚€(gè)數(shù)據(jù)相同,歸0后視覺上就像無縫滾動(dòng)。
<div style="height: 100%" @mouseenter="moveStar()" @mouseleave="moveLeave()">
? ? ? <table id="rollOne" border="1" :style="{transform:'translate(0px,'+flvPlayerTimer+'px)'}">
? ? ? ? <tr v-for="item in tableData" :key="item.index">
? ? ? ? ? <td width="25%">{{item.fxsj}}</td>
? ? ? ? ? <td width="15%">{{item.gjbh}}</td>
? ? ? ? ? <td width="35%">{{item.pzgs}}個(gè)</td>
? ? ? ? ? <td width="25%" style="cursor: pointer" @click="popu(2,item)"><span>詳情</span></td>
? ? ? ? </tr>
? ? ? </table>
? ? ? <table border="1" :style="{transform:'translate(0px,'+flvPlayerTimer+'px)'}">
? ? ? ? <tr v-for="item in tableData" :key="item.index">
? ? ? ? ? <td width="25%">{{item.fxsj}}</td>
? ? ? ? ? <td width="15%">{{item.gjbh}}</td>
? ? ? ? ? <td width="35%">{{item.pzgs}}個(gè)</td>
? ? ? ? ? <td width="25%" style="cursor: pointer" @click="popu(2,item)"><span>詳情</span></td>
? ? ? ? </tr>
? ? ? </table>
</div>js
export default {
? ? ? ? name: "rolling",
? ? ? ? data() {
? ? ? ? ? return {
? ? ? ? ? ? flvPlayerTimer:0,
? ? ? ? ? ? timer:null
? ? ? ? ? }
? ? ? ? },
? ? ? ? props: {
? ? ? ? ? tableData: {
? ? ? ? ? ? type: Array
? ? ? ? ? },
? ? ? ? },
? ? ? ? mounted(){
? ? ? ? ? this.timer = setInterval(()=>{
? ? ? ? ? ? this.flvPlayerTimer-=1
? ? ? ? ? ? if(this.flvPlayerTimer== -($('#rollOne').height())){
? ? ? ? ? ? ? this.flvPlayerTimer =0
? ? ? ? ? ? }
? ? ? ? ? },100)
? ? ? ? ? // 別忘了定時(shí)器清除
? ? ? ? ? this.$once('hook:beforeDestroy',()=>{
? ? ? ? ? ? clearInterval(this.timer);
? ? ? ? ? ? this.timer = null;
? ? ? ? ? })
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ?// 鼠標(biāo)觸碰停止
? ? ? ? ? moveStar(){
? ? ? ? ? ? clearInterval(this.timer);
? ? ? ? ? ? this.timer2 = null;
? ? ? ? ? },
? ? ? ? ? // 鼠標(biāo)離開始
? ? ? ? ? moveLeave(){
? ? ? ? ? ? this.timer = setInterval(()=>{
? ? ? ? ? ? ? this.flvPlayerTimer-=1
? ? ? ? ? ? ? if(this.flvPlayerTimer== -($('#rollOne').height())){
? ? ? ? ? ? ? ? this.flvPlayerTimer =0
? ? ? ? ? ? ? }
? ? ? ? ? ? },100)
? ? ? ? ? },
? ? ? ? }
? ? }css
.fxlx{
? ? height: 16vh;
? ? width: 100%;
? ? table,table tr td {
? ? ? border:1px solid ? rgba(41,143,229,0.3);
? ? }
? ? table{
? ? ? width: 90%;
? ? ? margin: 0 auto;
? ? ? th{
? ? ? ? opacity: 0.7;
? ? ? ? background: linear-gradient(rgba(53,123,203,0.7), rgba(9,57,113,0.7));
? ? ? ? font-size: 9rem;
? ? ? ? font-family: PingFang SC Regular, PingFang SC Regular-Regular;
? ? ? ? font-weight: 400;
? ? ? ? color: #ffffff;
? ? ? ? height: 28rem;
? ? ? }
? ? ? td{
? ? ? ? opacity: 0.8;
? ? ? ? font-size: 9rem;
? ? ? ? height: 30rem;
? ? ? ? font-family: PingFang SC Regular, PingFang SC Regular-Regular;
? ? ? ? font-weight: 400;
? ? ? ? color: #ffffff;
? ? ? ? background:#001c38
? ? ? }
? ? }
? }2、使用vue-seamless-scroll插件
1、安裝vue-seamless-scroll
npm install vue-seamless-scroll --save
2、引入組件
在某些時(shí)候?qū)嶋H頁面渲染后會(huì)出現(xiàn)點(diǎn)擊事件失效的情況。這個(gè)問題是因?yàn)関ue-seamless-scroll是用重復(fù)渲染一遍內(nèi)部元素來實(shí)現(xiàn)滾動(dòng)的,而JS的onclick只檢測頁面渲染時(shí)的DOM元素。記得在入門原生JS的時(shí)候也經(jīng)常會(huì)遇見這個(gè)問題,經(jīng)過一般百度,采用事件委托的方式解決。
在section上綁定事件handleClick,捕獲點(diǎn)擊的DOM節(jié)點(diǎn)。事件中需求的數(shù)據(jù)可以直接用data綁在相應(yīng)的dom上。
<div class="my-inbox" @click.stop="handleClick($event)">
? ? ? <vue-seamless-scroll :data="sendVal.body" :class-option="defaultOption">
? ? ? ? <!-- ? ? ? ?<div v-for="(item, index) in sendVal" :key="index" @click="jump(item)">-->
? ? ? ? <!-- ? ? ? ? ?<div class="wfjl1" v-show="index % 2 == 0">{{ item }}</div>-->
? ? ? ? <!-- ? ? ? ? ?<div class="wfjl2" v-show="index % 2 == 1">{{ item }}</div>-->
? ? ? ? <!-- ? ? ? ?</div>-->
? ? ? ? <table ref="movebox">
? ? ? ? ? <tr v-for="(item, index) in sendVal.body" :key="index">
? ? ? ? ? ? <td
? ? ? ? ? ? ? :data-obj="JSON.stringify(item)"
? ? ? ? ? ? ? :id="'xzq' + index"
? ? ? ? ? ? ? width="15%"
? ? ? ? ? ? >
? ? ? ? ? ? ? {{ item.range }}
? ? ? ? ? ? </td>
? ? ? ? ? ? <td
? ? ? ? ? ? ? :data-obj="JSON.stringify(item)"
? ? ? ? ? ? ? :id="'wflx' + index"
? ? ? ? ? ? ? width="20%"
? ? ? ? ? ? >
? ? ? ? ? ? ? {{ item.wflx }}
? ? ? ? ? ? </td>
? ? ? ? ? ? <td :data-obj="JSON.stringify(item)" :id="'sj' + index" width="25%">
? ? ? ? ? ? ? {{ item.sbsj }}
? ? ? ? ? ? </td>
? ? ? ? ? ? <td :data-obj="JSON.stringify(item)" :id="'zt' + index" width="20%">
? ? ? ? ? ? ? <img
? ? ? ? ? ? ? ? style="width: 71rem; height: 34rem; margin: 5rem 0"
? ? ? ? ? ? ? ? :src="item.image_result"
? ? ? ? ? ? ? />
? ? ? ? ? ? </td>
? ? ? ? ? </tr>
? ? ? ? </table>
? ? ? </vue-seamless-scroll>
</div>js
import vueSeamlessScroll from "vue-seamless-scroll";
export default {
? name: "my-marquee-top",
? props: {
? ? sendVal: Object,
? },
? data() {
? ? return {
? ? ? isShow: true,
? ? ? time: "",
? ? ? url: "",
? ? };
? },
? components: {
? ? vueSeamlessScroll,
? },
? computed: {
? ? defaultOption() {
? ? ? return {
? ? ? ? step: 0.2, // 數(shù)值越大速度滾動(dòng)越快
? ? ? ? limitMoveNum: 2, // 開始無縫滾動(dòng)的數(shù)據(jù)量 this.dataList.length
? ? ? ? hoverStop: true, // 是否開啟鼠標(biāo)懸停stop
? ? ? ? direction: 1, // 0向下 1向上 2向左 3向右
? ? ? ? openWatch: true, // 開啟數(shù)據(jù)實(shí)時(shí)監(jiān)控刷新dom/
? ? ? };
? ? },
? },
? methods: {
? ? handleClick(item) {
? ? ? let message = JSON.parse(item.target.dataset.obj);
? ? ? this.$emit("jump", message);
? ? },
? }
? },
};```以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vuejs中使用markdown服務(wù)器端渲染的示例
- JS加載解析Markdown文檔過程詳解
- js正則匹配markdown里的圖片標(biāo)簽的實(shí)現(xiàn)
- JavaScript+Node.js寫一款markdown解析器
- JavaScript滾動(dòng)輪播圖制作原理詳解
- js實(shí)現(xiàn)文字滾動(dòng)的效果
- js實(shí)現(xiàn)列表循環(huán)滾動(dòng)
- js實(shí)現(xiàn)列表自動(dòng)滾動(dòng)循環(huán)播放
- JavaScript markdown 編輯器實(shí)現(xiàn)雙屏同步滾動(dòng)
相關(guān)文章
javaScript實(shí)現(xiàn)滾動(dòng)條事件詳解
這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)滾動(dòng)條事件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
JS動(dòng)態(tài)修改圖片的URL(src)的方法
這篇文章主要介紹了JS動(dòng)態(tài)修改圖片的URL(src)的方法,涉及javascript操作圖片src屬性的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
echarts中X軸顯示特定個(gè)數(shù)label并修改樣式的方法詳解
最近在使用Echarts圖表遇到些特別的需求,想著給大家整理下,所以下面這篇文章主要給大家介紹了關(guān)于echarts中X軸顯示特定個(gè)數(shù)label并修改樣式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
自己編寫的支持Ajax驗(yàn)證的JS表單驗(yàn)證插件
創(chuàng)建一個(gè)JavaScript表單驗(yàn)證插件,可以說是一個(gè)繁瑣的過程,涉及到初期設(shè)計(jì)、開發(fā)與測試等等環(huán)節(jié)。實(shí)際上一個(gè)優(yōu)秀的程序員不僅是技術(shù)高手,也應(yīng)該是善假于外物的。本文介紹的這個(gè)不錯(cuò)的JavaScript表單驗(yàn)證插件,支持ajax驗(yàn)證,有需要的小伙伴可以參考下2015-05-05
js實(shí)現(xiàn)完美兼容各大瀏覽器的人民幣大小寫相互轉(zhuǎn)換
在基于網(wǎng)頁的打印輸出或報(bào)表中,經(jīng)常會(huì)牽扯到金額的大寫,每次都打上去很麻煩,所以想法用一個(gè)JavaScript客戶端腳本來實(shí)現(xiàn)自動(dòng)轉(zhuǎn)換,只需在需要顯示大寫金額的時(shí)候調(diào)用該JS函數(shù),下面我們就來匯總下吧2015-10-10
javascript巧用eval函數(shù)組裝表單輸入項(xiàng)為json對(duì)象的方法
這篇文章主要介紹了javascript巧用eval函數(shù)組裝表單輸入項(xiàng)為json對(duì)象的方法,實(shí)例分析了JavaScript使用eval函數(shù)動(dòng)態(tài)構(gòu)造json對(duì)象的相關(guān)技巧,需要的朋友可以參考下2015-11-11
js當(dāng)一個(gè)變量為函數(shù)時(shí) 應(yīng)該注意的一點(diǎn)細(xì)節(jié)小結(jié)
變量testFun為一個(gè)匿名函數(shù),匿名函數(shù)返回的一個(gè)testFun.init對(duì)象(也是一個(gè)匿名函數(shù))2011-12-12

