欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

參考

項目描述
GitHub前往
Vanilla-tilt.js前往

描述

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

獲取

npm install vanilla-tilt

vanilla-tilt

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

效果

特點

vanilla-tilt 存在以下特點:

  • 輕量級
  • 無依賴項
  • 使用簡單
  • 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

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

如下示例將設(shè)置 #card 元素為目標(biāo)元素并將其 max 配置選項的值設(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ù)接收兩個參數(shù),第一個參數(shù)為需要添加傾斜效果的元素對象,第二個參數(shù)則是用于添加傾斜效果的配置對象。

如下示例將設(shè)置 #card 元素為目標(biāo)元素并將其 max 配置選項的值設(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)先級

當(dāng)使用 data-tilt-{option}VanillaTilt.init() 同時對配置選項進行設(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)元素之上時,將目標(biāo)元素放縮指定倍數(shù) 
        })
    </script>
</body>
</html>

效果:

效果

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

配置選項

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

其他

配置選項中還存在其他選項,但目前這些選項我并沒有弄清楚他們的用法。為避免誤人子弟,我并沒有對這些選項進行翻譯,請見諒。

項目默認(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庫之vanilla-tilt使用教程的文章就介紹到這了,更多相關(guān)JS庫vanilla-tilt使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • prototype.js簡單實現(xiàn)ajax功能示例

    prototype.js簡單實現(xiàn)ajax功能示例

    這篇文章主要介紹了prototype.js簡單實現(xiàn)ajax功能,結(jié)合實例形式分析了prototype.js前臺實現(xiàn)ajax與后臺struts的相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • 微信小程序CSS3動畫下拉菜單效果

    微信小程序CSS3動畫下拉菜單效果

    這篇文章主要為大家詳細介紹了微信小程序CSS3動畫下拉菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 微信小程序自定義聯(lián)系人彈窗

    微信小程序自定義聯(lián)系人彈窗

    這篇文章主要為大家詳細介紹了微信小程序自定義聯(lián)系人彈窗,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • JavaScript生成一個不重復(fù)的ID的方法示例

    JavaScript生成一個不重復(fù)的ID的方法示例

    這篇文章主要介紹了JavaScript生成一個不重復(fù)的ID的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 淺析JavaScript定時器setTimeout的時延問題

    淺析JavaScript定時器setTimeout的時延問題

    這篇文章主要為大家詳細介紹了JavaScript中定時器setTimeout有最小時延的相關(guān)知識,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • javascript使用smipleChart實現(xiàn)簡單圖表

    javascript使用smipleChart實現(xiàn)簡單圖表

    這篇文章主要介紹了javascript使用smipleChart實現(xiàn)簡單圖表的方法及示例分享,需要的朋友可以參考下
    2015-01-01
  • bootstrap3 dialog 更強大、更靈活的模態(tài)框

    bootstrap3 dialog 更強大、更靈活的模態(tài)框

    這篇文章主要介紹了bootstrap3 dialog 更強大、更靈活的模態(tài)框,本文通過效果展示實例代碼詳解,需要的朋友可以參考下
    2017-04-04
  • 微信小程序?qū)崿F(xiàn)走馬燈效果實例

    微信小程序?qū)崿F(xiàn)走馬燈效果實例

    這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)走馬燈效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 詳解JavaScript如何利用異步解密回調(diào)地獄

    詳解JavaScript如何利用異步解密回調(diào)地獄

    為了更好地處理這些異步操作,JavaScript?引入了異步編程的概念,這篇文章主要來和大家詳細聊聊JavaScript中異步的相關(guān)應(yīng)用,希望對大家有所幫助
    2024-02-02
  • javascript-簡單的計算器實現(xiàn)步驟分解(附圖)

    javascript-簡單的計算器實現(xiàn)步驟分解(附圖)

    輸入內(nèi)容的判斷,對于事件對象的來源的判斷以及數(shù)學(xué)運算“+,-,*,/”的使用,感興趣的朋友可以學(xué)習(xí)下
    2013-05-05

最新評論