最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript庫(kù)之vanilla-tilt使用教程(一個(gè)平滑的3D傾斜庫(kù))

 更新時(shí)間:2023年02月13日 10:21:46   作者:兩個(gè)月亮  
vanilla-tilt.js是Javascript中一個(gè)平滑的3D傾斜庫(kù),可以讓網(wǎng)頁(yè)的一些控件變得動(dòng)態(tài)起來(lái),下面這篇文章主要給大家介紹了關(guān)于JavaScript庫(kù)之vanilla-tilt使用的相關(guān)資料,需要的朋友可以參考下

參考

項(xiàng)目描述
GitHub前往
Vanilla-tilt.js前往

描述

項(xiàng)目描述
操作系統(tǒng)Windwos 10 專(zhuān)業(yè)版
Edge108.0.1462.54 (正式版本) (64 位)
vanilla-tilt.js1.8.0

獲取

npm install vanilla-tilt

vanilla-tilt

vanilla-tilt.js 是 JavaScript 中的一個(gè)平滑的 3D 傾斜庫(kù),該庫(kù)存在 JQuery 版本——Tilt.js 。

效果

特點(diǎn)

vanilla-tilt 存在以下特點(diǎn):

  • 輕量級(jí)
  • 無(wú)依賴(lài)項(xiàng)
  • 使用簡(jiǎn)單
  • 60 FPS
  • 絲滑

使用

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vanilla-tilt</title>
    <style>
        *{
            /* 去除元素默認(rèn)的內(nèi)外邊距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 設(shè)置顯示區(qū)域的最小高度值為顯示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中顯示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 為目標(biāo)元素指定寬高并為其設(shè)置漸變背景顏色 */
            width: 200px;
            height: 200px;
            background: linear-gradient(to right bottom, rgb(108, 240, 255), rgb(118, 255, 180));
        }
    </style>
</head>
<body>
    <!-- 需要添加 3D 傾斜特效的元素 -->
    <div id="card"></div>

	<!-- 導(dǎo)入 vanilla-tilt.js -->
	<script src="./vanilla-tilt.js"></script>
	<script>
    	VanillaTilt.init(document.querySelector('#card'), {
        	max: 15 // 設(shè)置傾斜的最大角度
    	});
	</script>
</body>
</html>

效果:

效果

使用

為目標(biāo)元素應(yīng)用傾斜樣式可以有兩種方式。

1. data-tilt

我們可以通過(guò)為元素添加屬性 data-tilt 來(lái)指定該元素為目標(biāo)元素并為其應(yīng)用默認(rèn)的傾斜配置。如果對(duì)默認(rèn)的傾斜配置中的某個(gè)選項(xiàng)不滿(mǎn),需要對(duì)其進(jìn)行更換,則可以通過(guò)為目標(biāo)元素添加合適的屬性并為其設(shè)置滿(mǎn)意的屬性值即可。

如下示例將設(shè)置 #card 元素為目標(biāo)元素并將其 max 配置選項(xiàng)的值設(shè)置為 25

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vanilla-tilt</title>
    <style>
        *{
            /* 去除元素默認(rèn)的內(nèi)外邊距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 設(shè)置顯示區(qū)域的最小高度值為顯示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中顯示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 為目標(biāo)元素指定寬高并為其設(shè)置漸變背景顏色 */
            width: 200px;
            height: 200px;
            background: linear-gradient(to right bottom, rgb(108, 240, 255), rgb(118, 255, 180));
        }
    </style>
</head>
<body>
    <!-- 需要添加 3D 傾斜特效的元素 -->
    <div id="card" data-tilt data-tilt-max="25"></div>

	<!-- 導(dǎo)入 vanilla-tilt.js -->
	<script src="./vanilla-tilt.js"></script>
</body>
</html>

效果:

效果

2. VanillaTilt.init()

VanillaTilt.init() 函數(shù)接收兩個(gè)參數(shù),第一個(gè)參數(shù)為需要添加傾斜效果的元素對(duì)象,第二個(gè)參數(shù)則是用于添加傾斜效果的配置對(duì)象。

如下示例將設(shè)置 #card 元素為目標(biāo)元素并將其 max 配置選項(xiàng)的值設(shè)置為 25

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vanilla-tilt</title>
    <style>
        *{
            /* 去除元素默認(rèn)的內(nèi)外邊距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 設(shè)置顯示區(qū)域的最小高度值為顯示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中顯示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 為目標(biāo)元素指定寬高并為其設(shè)置漸變背景顏色 */
            width: 200px;
            height: 200px;
            background: linear-gradient(to right bottom, rgb(108, 240, 255), rgb(118, 255, 180));
        }
    </style>
</head>
<body>
    <!-- 需要添加 3D 傾斜特效的元素 -->
    <div id="card"></div>

    <!-- 導(dǎo)入 vanilla-tilt.js -->
    <script src="./vanilla-tilt.js"></script>
    <script>
        VanillaTilt.init(document.querySelector('#card'), {
            max: 25
        })
    </script>
</body>
</html>

優(yōu)先級(jí)

當(dāng)使用 data-tilt-{option}VanillaTilt.init() 同時(shí)對(duì)配置選項(xiàng)進(jìn)行設(shè)置時(shí),將優(yōu)先使用 data-tilt-{option} 提供的配置,VanillaTilt.init() 的所有配置都將失效。

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vanilla-tilt</title>
    <style>
        *{
            /* 去除元素默認(rèn)的內(nèi)外邊距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 設(shè)置顯示區(qū)域的最小高度值為顯示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中顯示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 為目標(biāo)元素指定寬高并為其設(shè)置漸變背景顏色 */
            width: 200px;
            height: 200px;
            background: linear-gradient(to right bottom, rgb(108, 240, 255), rgb(118, 255, 180));
        }
    </style>
</head>
<body>
    <!-- 需要添加 3D 傾斜特效的元素 -->
    <div id="card" data-tilt data-tilt-max="70"></div>

    <!-- 導(dǎo)入 vanilla-tilt.js -->
    <script src="./vanilla-tilt.js"></script>
    <script>
        VanillaTilt.init(document.querySelector('#card'), {
            max: 10,
            scale: 2 // 在鼠標(biāo)懸停于目標(biāo)元素之上時(shí),將目標(biāo)元素放縮指定倍數(shù) 
        })
    </script>
</body>
</html>

效果:

效果

可以看到目標(biāo)元素使用了 data-tilt-max 所設(shè)定的配置選項(xiàng)的值,忽視了 VanillaTilt.init() 提供的 maxscale 。

配置選項(xiàng)

項(xiàng)目默認(rèn)值描述
reversefalse反轉(zhuǎn)傾斜方向(設(shè)置為 true 時(shí),鼠標(biāo)在目標(biāo)元素懸浮時(shí)該處會(huì)向屏幕內(nèi)測(cè)傾斜,默認(rèn)向屏幕外側(cè)傾斜)
max35最大傾斜角度。
scale1設(shè)置鼠標(biāo)懸浮于目標(biāo)元素時(shí),目標(biāo)元素的放縮倍數(shù)。
glarefalse如果設(shè)置為 True,則會(huì)在鼠標(biāo)懸停的位置制造反光效果,反光效果僅出現(xiàn)在目標(biāo)元素的下面部分。
max-glare1設(shè)置反光強(qiáng)度,取值范圍為 0~1,該配置選項(xiàng)的值越是接近于 1 反光強(qiáng)度越大。反光強(qiáng)度的大小可以理解為 照到目標(biāo)元素的那束光的光照強(qiáng)度 。該配置選項(xiàng)為 0 時(shí)與 glare 設(shè)置為 false 時(shí)的效果無(wú)異。
axisnull設(shè)置被激活的坐標(biāo)軸,被禁用的坐標(biāo)軸將不會(huì)產(chǎn)生傾斜效果。該配置選項(xiàng)的取值有 x、ynull。其中 null 表示同時(shí)激活 xy 軸。
resettrue當(dāng)該選項(xiàng)設(shè)置為 false 時(shí),鼠標(biāo)若離開(kāi)目標(biāo)元素,目標(biāo)元素將維持鼠標(biāo)離開(kāi)前的狀態(tài)(傾斜狀態(tài)及放縮狀態(tài))。若該選項(xiàng)設(shè)置為 true,鼠標(biāo)若離開(kāi)目標(biāo)元素,目標(biāo)元素將被去除傾斜狀態(tài)及放縮狀態(tài)。
startX0設(shè)置目標(biāo)元素在 x 軸上的初始默認(rèn)狀態(tài)。若要使用該選項(xiàng),需要保證配置選項(xiàng) resetreset-to-start 的值均為 true 。
startY0設(shè)置目標(biāo)元素在 y 軸上的初始默認(rèn)狀態(tài)。若要使用該選項(xiàng),需要保證配置選項(xiàng) resetreset-to-start 的值均為 true
reset-to-starttrue若該選項(xiàng)設(shè)置為 true,鼠標(biāo)離開(kāi)目標(biāo)元素時(shí),目標(biāo)元素的傾斜狀態(tài)將恢復(fù)至配置選項(xiàng) startXstartY 指定的傾斜狀態(tài)。
full-page-listeningfalse當(dāng)該配置選項(xiàng)設(shè)置為 true 時(shí),目標(biāo)元素將響應(yīng)當(dāng)前頁(yè)面的任何鼠標(biāo)移動(dòng)(鼠標(biāo)即使沒(méi)有懸停在目標(biāo)元素中,也可以通過(guò)鼠標(biāo)移動(dòng)控制目標(biāo)元素的傾斜狀態(tài))。

其他

配置選項(xiàng)中還存在其他選項(xiàng),但目前這些選項(xiàng)我并沒(méi)有弄清楚他們的用法。為避免誤人子弟,我并沒(méi)有對(duì)這些選項(xiàng)進(jìn)行翻譯,請(qǐng)見(jiàn)諒。

項(xiàng)目默認(rèn)值描述
gyroscopetrueBoolean to enable/disable device orientation detection.
gyroscopeMinAngleX-45This is the bottom limit of the device angle on X axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the left border of the element.
gyroscopeMaxAngleX45This is the top limit of the device angle on X axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the right border of the element.
gyroscopeMinAngleY-45This is the bottom limit of the device angle on Y axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the top border of the element.
gyroscopeMaxAngleY45This is the top limit of the device angle on Y axis, meaning that a device rotated at this angle would tilt the element as if the mouse was on the bottom border of the element.
mouse-event-elementnullcss-selector or link to HTML-element what will be listen mouse events.
glare-prerenderfalsefalse = VanillaTilt creates the glare elements for you, otherwise you need to add .js-tilt-glare>.js-tilt-glare-inner by yourself.
easingcubic-bezier(.03,.98,.52,.99)Easing on enter/exit.
speed300Speed of the enter/exit transition.
perspective1000Transform perspective, the lower the more extreme the tilt gets.
transitiontrueSet a transition on enter/exit.

總結(jié)

到此這篇關(guān)于JavaScript庫(kù)之vanilla-tilt使用教程的文章就介紹到這了,更多相關(guān)JS庫(kù)vanilla-tilt使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 純JS實(shí)現(xiàn)監(jiān)控本地文件變化

    純JS實(shí)現(xiàn)監(jiān)控本地文件變化

    你是否曾夢(mèng)想擁有一個(gè)能夠?qū)崟r(shí)監(jiān)控本地文件變化的網(wǎng)頁(yè)應(yīng)用,現(xiàn)在,這個(gè)夢(mèng)想即將成為現(xiàn)實(shí),本文將通過(guò)純JS實(shí)現(xiàn)這一功能,感興趣的小伙伴可以了解下
    2025-04-04
  • layui 實(shí)現(xiàn)表格某一列顯示圖標(biāo)

    layui 實(shí)現(xiàn)表格某一列顯示圖標(biāo)

    今天小編就為大家分享一篇layui 實(shí)現(xiàn)表格某一列顯示圖標(biāo)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09
  • 原生js添加節(jié)點(diǎn)appendChild、insertBefore方式

    原生js添加節(jié)點(diǎn)appendChild、insertBefore方式

    這篇文章主要介紹了原生js添加節(jié)點(diǎn)appendChild、insertBefore方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • JS 實(shí)現(xiàn)可停頓的垂直滾動(dòng)實(shí)例代碼

    JS 實(shí)現(xiàn)可停頓的垂直滾動(dòng)實(shí)例代碼

    下面小編就為大家?guī)?lái)一篇JS 實(shí)現(xiàn)可停頓的垂直滾動(dòng)實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊飄愛(ài)心效果

    js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊飄愛(ài)心效果

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊飄愛(ài)心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Jquery+javascript實(shí)現(xiàn)支付網(wǎng)頁(yè)數(shù)字鍵盤(pán)

    Jquery+javascript實(shí)現(xiàn)支付網(wǎng)頁(yè)數(shù)字鍵盤(pán)

    這篇文章主要為大家詳細(xì)介紹了Jquery+javascript實(shí)現(xiàn)支付網(wǎng)頁(yè)數(shù)字鍵盤(pán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • document.all與getElementById、getElementsByName、getElementsByTagName用法區(qū)別-getElementById

    document.all與getElementById、getElementsByName、getElementsByT

    HTML DOM 定義了多種查找元素的方法,除了 getElementById() 之外,還有 getElementsByName() 和 getElementsByTagName()。
    2008-12-12
  • JavaScript數(shù)組reduce常見(jiàn)實(shí)例方法

    JavaScript數(shù)組reduce常見(jiàn)實(shí)例方法

    reduce方法在數(shù)組的每個(gè)元素上執(zhí)行用戶(hù)提供的回調(diào)函數(shù),即"reducer",它傳入對(duì)前一個(gè)元素進(jìn)行計(jì)算的返回值,結(jié)果是單個(gè)值,它是在數(shù)組的所有元素上運(yùn)行reducer的結(jié)果,下面這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組reduce常見(jiàn)實(shí)例方法的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • javascript實(shí)現(xiàn)移動(dòng)端觸屏拖拽功能

    javascript實(shí)現(xiàn)移動(dòng)端觸屏拖拽功能

    這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)移動(dòng)端觸屏拖拽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 關(guān)于前端小程序中.env?文件夾示例詳解

    關(guān)于前端小程序中.env?文件夾示例詳解

    這篇文章主要給大家介紹了關(guān)于前端小程序中.env?文件夾的相關(guān)資料,.env文件夾允許開(kāi)發(fā)者在不同的環(huán)境中配置不同的變量值,以便在小程序的不同階段或環(huán)境中使用,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05

最新評(píng)論

巴林左旗| 饶河县| 昆明市| 乐山市| 永丰县| 正宁县| 牟定县| 英山县| 江门市| 东丽区| 保定市| 勃利县| 哈巴河县| 禄丰县| 兰坪| 裕民县| 昌江| 班玛县| 军事| 鹤壁市| 买车| 洛宁县| 平湖市| 石阡县| 新建县| 绍兴市| 元朗区| 志丹县| 抚松县| 东兴市| 湖口县| 宁城县| 上高县| 灵寿县| 景洪市| 西峡县| 故城县| 苏州市| 江陵县| 东宁县| 临西县|