JavaScript库之vanilla-tilt使用教程(一个平滑的3D倾斜库)

 更新时间:2023年02月13日 10:21:46   作者:两个月亮  
vanilla-tilt.js是Javascript中一个平滑的3D倾斜库,可以让网页的一些控件变得动态起来,下面这篇文章主要给大家介绍了关于JavaScript库之vanilla-tilt使用的相关资料,需要的朋友可以参考下

参考

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

描述

项目描述
操作系统Windwos 10 专业版
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>
        *{
            /* 去除元素默认的内外边距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 设置显示区域的最小高度值为显示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中显示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 为目标元素指定宽高并为其设置渐变背景颜色 */
            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>

	<!-- 导入 vanilla-tilt.js -->
	<script src="./vanilla-tilt.js"></script>
	<script>
    	VanillaTilt.init(document.querySelector('#card'), {
        	max: 15 // 设置倾斜的最大角度
    	});
	</script>
</body>
</html>

效果:

效果

使用

为目标元素应用倾斜样式可以有两种方式。

1. data-tilt

我们可以通过为元素添加属性 data-tilt 来指定该元素为目标元素并为其应用默认的倾斜配置。如果对默认的倾斜配置中的某个选项不满,需要对其进行更换,则可以通过为目标元素添加合适的属性并为其设置满意的属性值即可。

如下示例将设置 #card 元素为目标元素并将其 max 配置选项的值设置为 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>
        *{
            /* 去除元素默认的内外边距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 设置显示区域的最小高度值为显示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中显示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 为目标元素指定宽高并为其设置渐变背景颜色 */
            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>

	<!-- 导入 vanilla-tilt.js -->
	<script src="./vanilla-tilt.js"></script>
</body>
</html>

效果:

效果

2. VanillaTilt.init()

VanillaTilt.init() 函数接收两个参数,第一个参数为需要添加倾斜效果的元素对象,第二个参数则是用于添加倾斜效果的配置对象。

如下示例将设置 #card 元素为目标元素并将其 max 配置选项的值设置为 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>
        *{
            /* 去除元素默认的内外边距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 设置显示区域的最小高度值为显示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中显示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 为目标元素指定宽高并为其设置渐变背景颜色 */
            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>

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

优先级

当使用 data-tilt-{option}VanillaTilt.init() 同时对配置选项进行设置时,将优先使用 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>
        *{
            /* 去除元素默认的内外边距 */
            margin: 0px;
            padding: 0px;
        }

        body{
            /* 设置显示区域的最小高度值为显示窗口的高度值 */
            min-height: 100vh;
            /* 使 body 中的元素居中显示 */
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        #card{
            /* 为目标元素指定宽高并为其设置渐变背景颜色 */
            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>

    <!-- 导入 vanilla-tilt.js -->
    <script src="./vanilla-tilt.js"></script>
    <script>
        VanillaTilt.init(document.querySelector('#card'), {
            max: 10,
            scale: 2 // 在鼠标悬停于目标元素之上时,将目标元素放缩指定倍数 
        })
    </script>
</body>
</html>

效果:

效果

可以看到目标元素使用了 data-tilt-max 所设定的配置选项的值,忽视了 VanillaTilt.init() 提供的 maxscale

配置选项

项目默认值描述
reversefalse反转倾斜方向(设置为 true 时,鼠标在目标元素悬浮时该处会向屏幕内测倾斜,默认向屏幕外侧倾斜)
max35最大倾斜角度。
scale1设置鼠标悬浮于目标元素时,目标元素的放缩倍数。
glarefalse如果设置为 True,则会在鼠标悬停的位置制造反光效果,反光效果仅出现在目标元素的下面部分。
max-glare1设置反光强度,取值范围为 0~1,该配置选项的值越是接近于 1 反光强度越大。反光强度的大小可以理解为 照到目标元素的那束光的光照强度 。该配置选项为 0 时与 glare 设置为 false 时的效果无异。
axisnull设置被激活的坐标轴,被禁用的坐标轴将不会产生倾斜效果。该配置选项的取值有 xynull。其中 null 表示同时激活 xy 轴。
resettrue当该选项设置为 false 时,鼠标若离开目标元素,目标元素将维持鼠标离开前的状态(倾斜状态及放缩状态)。若该选项设置为 true,鼠标若离开目标元素,目标元素将被去除倾斜状态及放缩状态。
startX0设置目标元素在 x 轴上的初始默认状态。若要使用该选项,需要保证配置选项 resetreset-to-start 的值均为 true
startY0设置目标元素在 y 轴上的初始默认状态。若要使用该选项,需要保证配置选项 resetreset-to-start 的值均为 true
reset-to-starttrue若该选项设置为 true,鼠标离开目标元素时,目标元素的倾斜状态将恢复至配置选项 startXstartY 指定的倾斜状态。
full-page-listeningfalse当该配置选项设置为 true 时,目标元素将响应当前页面的任何鼠标移动(鼠标即使没有悬停在目标元素中,也可以通过鼠标移动控制目标元素的倾斜状态)。

其他

配置选项中还存在其他选项,但目前这些选项我并没有弄清楚他们的用法。为避免误人子弟,我并没有对这些选项进行翻译,请见谅。

项目默认值描述
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.

总结

到此这篇关于JavaScript库之vanilla-tilt使用教程的文章就介绍到这了,更多相关JS库vanilla-tilt使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 纯JS实现监控本地文件变化

    纯JS实现监控本地文件变化

    你是否曾梦想拥有一个能够实时监控本地文件变化的网页应用,现在,这个梦想即将成为现实,本文将通过纯JS实现这一功能,感兴趣的小伙伴可以了解下
    2025-04-04
  • layui 实现表格某一列显示图标

    layui 实现表格某一列显示图标

    今天小编就为大家分享一篇layui 实现表格某一列显示图标的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • 原生js添加节点appendChild、insertBefore方式

    原生js添加节点appendChild、insertBefore方式

    这篇文章主要介绍了原生js添加节点appendChild、insertBefore方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • JS 实现可停顿的垂直滚动实例代码

    JS 实现可停顿的垂直滚动实例代码

    下面小编就为大家带来一篇JS 实现可停顿的垂直滚动实例代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • js实现鼠标点击飘爱心效果

    js实现鼠标点击飘爱心效果

    这篇文章主要为大家详细介绍了js实现鼠标点击飘爱心效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • Jquery+javascript实现支付网页数字键盘

    Jquery+javascript实现支付网页数字键盘

    这篇文章主要为大家详细介绍了Jquery+javascript实现支付网页数字键盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • document.all与getElementById、getElementsByName、getElementsByTagName用法区别-getElementById

    document.all与getElementById、getElementsByName、getElementsByT

    HTML DOM 定义了多种查找元素的方法,除了 getElementById() 之外,还有 getElementsByName() 和 getElementsByTagName()。
    2008-12-12
  • JavaScript数组reduce常见实例方法

    JavaScript数组reduce常见实例方法

    reduce方法在数组的每个元素上执行用户提供的回调函数,即"reducer",它传入对前一个元素进行计算的返回值,结果是单个值,它是在数组的所有元素上运行reducer的结果,下面这篇文章主要给大家介绍了关于JavaScript数组reduce常见实例方法的相关资料,需要的朋友可以参考下
    2022-05-05
  • javascript实现移动端触屏拖拽功能

    javascript实现移动端触屏拖拽功能

    这篇文章主要为大家详细介绍了javascript实现移动端触屏拖拽功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • 关于前端小程序中.env 文件夹示例详解

    关于前端小程序中.env 文件夹示例详解

    这篇文章主要给大家介绍了关于前端小程序中.env 文件夹的相关资料,.env文件夹允许开发者在不同的环境中配置不同的变量值,以便在小程序的不同阶段或环境中使用,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-05-05

最新评论