基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果
1.首先創(chuàng)建一個(gè)xScroll.vue組件
<template> <div class="main" v-size-ob="mainSize"> <div class="v-scroll"> <div class="content"> <slot></slot> </div> </div> </div> </template> <script setup lang="ts"> </script> <style scoped lang="less"> .main { width: 100%; height: 100%; } </style>
在頁(yè)面中使用
<div class="zlcXScroll"> <xScroll> <div class="imgs"> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> <div> <img src="https://picsum.photos/200/300" alt="" /> </div> </div> </xScroll> </div>
效果:
將容器內(nèi)的內(nèi)容橫向排列
.zlcXScroll { width: 100%; height: 300px; .imgs { display: flex; flex-direction: row; align-items: center; img { margin-right: 10px; } } }
效果:(目前需要按住shift再滑動(dòng)滾輪)
2.利用盒子縱向滾動(dòng)條默認(rèn)不需要shift鍵
綠色盒子就是v-scroll,v-scroll網(wǎng)上轉(zhuǎn)90之前,需要將內(nèi)容(照片盒子content)向下旋轉(zhuǎn)90°
3.獲取紅色盒子的寬高,賦值給綠色盒子的高寬
<template> <div class="main" v-size-ob="mainSize"> <div class="v-scroll"> <div class="content"> <slot></slot> </div> </div> </div> </template> <script setup lang="ts"> import { reactive } from "vue"; let mainMes = reactive({ w: 0, h: 0, }); interface Eobj { width: number; height: number; } let mainSize = (e: Eobj) => { let { width, height } = e; mainMes.w = width; mainMes.h = height; }; </script> <style scoped lang="less"> .main { width: 100%; height: 100%; outline: 1px solid red; .v-scroll { outline: 1px solid rgb(17, 0, 255); --w: calc(v-bind(mainMes.w) * 1px); --h: calc(v-bind(mainMes.h) * 1px); width: var(--h); height: var(--w); .content{ height: var(--h); } } } </style>
4.將content盒子向下旋轉(zhuǎn)90°
按照左上角為中心,直接旋轉(zhuǎn),會(huì)跑到最大容器外面去,可以往右移一點(diǎn)距離(紅色容器的高度),然后再旋轉(zhuǎn)
<template> <div class="main" v-size-ob="mainSize"> <div class="v-scroll"> <div class="content"> <slot></slot> </div> </div> </div> </template> <script setup lang="ts"> import { reactive } from "vue"; let mainMes = reactive({ w: 0, h: 0, }); interface Eobj { width: number; height: number; } let mainSize = (e: Eobj) => { let { width, height } = e; mainMes.w = width; mainMes.h = height; }; </script> <style scoped lang="less"> .main { width: 100%; height: 100%; outline: 1px solid red; .v-scroll { outline: 1px solid rgb(17, 0, 255); --w: calc(v-bind(mainMes.w) * 1px); --h: calc(v-bind(mainMes.h) * 1px); width: var(--h); height: var(--w); position: relative; .content { height: var(--h); position: absolute; left: var(--h); transform-origin: left top; transform: rotate(90deg); } } } </style>
效果:
5.content旋轉(zhuǎn)完后,再將v-scroll向上旋轉(zhuǎn)90°即可
<template> <div class="main" v-size-ob="mainSize"> <div class="v-scroll"> <div class="content"> <slot></slot> </div> </div> </div> </template> <script setup lang="ts"> import { reactive } from "vue"; let mainMes = reactive({ w: 0, h: 0, }); interface Eobj { width: number; height: number; } let mainSize = (e: Eobj) => { let { width, height } = e; mainMes.w = width; mainMes.h = height; }; </script> <style scoped lang="less"> .main { width: 100%; height: 100%; outline: 1px solid red; .v-scroll { outline: 1px solid rgb(17, 0, 255); --w: calc(v-bind(mainMes.w) * 1px); --h: calc(v-bind(mainMes.h) * 1px); width: var(--h); height: var(--w); position: relative; overflow: hidden scroll; transform-origin: 0 0; transform: translateY(var(--h)) rotate(-90deg); &::-webkit-scrollbar { width: 0; height: 0; } .content { height: var(--h); position: absolute; left: var(--h); transform-origin: left top; transform: rotate(90deg); } } } </style>
效果:(現(xiàn)在不需要按住shift,可直接滑動(dòng)滾輪實(shí)現(xiàn)頁(yè)面橫向滑動(dòng))
6.v-size-ob是自定義指令:(獲取元素的寬高)
新建一個(gè)sizeOb.ts文件
const map = new WeakMap(); const ob = new ResizeObserver((entries) => { for (const entry of entries) { const handler = map.get(entry.target); if (handler) { const box = entry.borderBoxSize[0]; handler({ width: box.inlineSize, height: box.blockSize, }); } } }); export default { mounted(el, binding) { ob.observe(el); map.set(el, binding.value); }, unmounted(el) { ob.unobserve(el); }, };
在main.ts入口文件中引入
import { createApp } from "vue"; import "./style.css"; import App from "./App.vue"; import Antd from "ant-design-vue"; import "ant-design-vue/dist/reset.css"; import sizeOb from "./directives/sizeOb"; let app = createApp(App); app.config.globalProperties.msg = "hello"; //自定義指令 app.directive("size-ob", sizeOb); app.use(Antd).mount("#app");
以上就是基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果的詳細(xì)內(nèi)容,更多關(guān)于Vue實(shí)現(xiàn)鼠頁(yè)面橫向滑動(dòng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue?使用addRoutes動(dòng)態(tài)添加路由及刷新頁(yè)面跳轉(zhuǎn)404路由的問(wèn)題解決方案
我自己使用addRoutes動(dòng)態(tài)添加的路由頁(yè)面,使用router-link標(biāo)簽可以跳轉(zhuǎn),但是一刷新就會(huì)自動(dòng)跳轉(zhuǎn)到我定義的通配符?*?指向的404路由頁(yè)面,這說(shuō)明沒(méi)有找到指定路由才跳到404路由的,這樣的情況如何處理呢,下面小編給大家分享解決方案,一起看看吧2023-10-10vue3.0關(guān)閉eslint校驗(yàn)的3種方法詳解
這篇文章主要給大家介紹了關(guān)于vue3.0關(guān)閉eslint校驗(yàn)的3種方法,在實(shí)際開(kāi)發(fā)過(guò)程中,eslint的作用不可估量,文中將關(guān)閉的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06基于Vue組件化的日期聯(lián)動(dòng)選擇器功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于Vue組件化的日期聯(lián)動(dòng)選擇器的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Vue3使用Element?Plus實(shí)現(xiàn)列表界面的方法步驟
寫后臺(tái)管理的時(shí)候會(huì)有很多列表以及相應(yīng)的條件查詢,下面這篇文章主要給大家介紹了關(guān)于Vue3使用Element?Plus實(shí)現(xiàn)列表界面的方法步驟,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Vue.js處理API請(qǐng)求失敗的最佳實(shí)踐和策略
在現(xiàn)代Web開(kāi)發(fā)中,與后端API的交互是不可避免的,然而,網(wǎng)絡(luò)請(qǐng)求是不穩(wěn)定的,可能會(huì)因?yàn)楦鞣N原因失敗,因此,優(yōu)雅地處理API請(qǐng)求失敗的情況是提升用戶體驗(yàn)和應(yīng)用穩(wěn)定性的關(guān)鍵,本文將詳細(xì)介紹在Vue.js中處理API請(qǐng)求失敗的最佳實(shí)踐和策略,需要的朋友可以參考下2024-12-12