Atropos 與支持以下功能的瀏覽器兼容
我們可以從 NPM 安裝 Atropos
npm i atropos
首先,我們需要包含 Atropos 樣式表
<link rel="stylesheet" href="path/to/atropos.css" />
使用捆綁器(如 webpack),您可以直接從 JavaScript 導入樣式
// main.jsimport 'atropos/css'
如果您使用 SCSS 預處理器,您可以通過以下方式從 SCSS 導入 Atropos 的樣式
// main.scss@import 'atropos/scss'
Atropos 提供了不同格式的樣式表。以下文件可用
atropos.css
(導出為 atropos/css
) - 所有 Atropos 樣式atropos.min.css
(導出為 atropos/css/min
) - 縮小的 Atropos 樣式atropos.scss
(導出為 atropos/scss
) - SCSS 語法中的 Atropos 樣式atropos.less
(導出為 atropos/less
) - LESS 語法中的 Atropos 樣式現在,我們需要將基本的 Atropos 佈局添加到我們的應用程序中
<!-- main Atropos container (required), add your custom class here --><div class="atropos my-atropos"><!-- scale container (required) --><div class="atropos-scale"><!-- rotate container (required) --><div class="atropos-rotate"><!-- inner container (required) --><div class="atropos-inner"><!-- put your custom content here --></div></div></div></div>
現在我們需要初始化 Atropos
// import Atropos libraryimport Atropos from 'atropos';// Initializeconst myAtropos = Atropos({el: '.my-atropos',// rest of parameters});
如果您在沒有 ES 模塊的環境中使用它,您可以通過 <script>
標籤添加 Atropos 庫
<script src="path/to/atropos.min.js"></script><script>const myAtropos = Atropos({el: '.my-atropos',// rest of parameters});</script>
如果您直接在瀏覽器中使用模塊,則可以從 CDN 導入它
import Atropos from 'https://cdn.jsdelivr.net/npm/atropos@2/atropos.min.mjs';const myAtropos = Atropos({el: '.my-atropos',// rest of parameters});
除了 Atropos 的主要 CSS 樣式外,我們可能還需要添加一些自定義樣式,例如設置 Atropos 大小
.my-atropos {width: 320px;height: 160px;}
名稱 | 類型 | 默認值 | 描述 |
---|---|---|---|
el | HTMLElement | string | Atropos 元素 | |
eventsEl | HTMLElement | string | 指標事件目標。如果未指定,事件處理程序將附加到主容器 (el ) | |
activeOffset | number | 50 | 激活時(懸停時)主容器偏移量,基本上表示 translateZ 值 |
alwaysActive | boolean | false | 使 Atropos 始終處於活動狀態 |
duration | number | 300 | 激活、離開和旋轉期間的過渡持續時間(以毫秒為單位) |
rotate | boolean | true | 定義是否在指標移動時旋轉容器(激活時) |
rotateTouch | boolean | string | true | 定義是否在觸摸移動時旋轉容器(激活時),必須啟用 rotate 。也接受特殊值
|
rotateXMax | number | 15 | 沿 X 軸的最大旋轉角度(以度為單位) |
rotateYMax | number | 15 | 沿 Y 軸的最大旋轉角度(以度為單位) |
rotateXInvert | boolean | false | 反轉沿 X 軸的旋轉(以度為單位) |
rotateYInvert | boolean | false | 反轉沿 Y 軸的旋轉(以度為單位) |
stretchX | number | 0 | 在此值上沿 X 軸移動(平移)atropos 容器(以百分比表示)。例如,`50` 值將移動容器寬度的 +-50%。僅適用於多個 Atropos,當多個 Atropos 使用相同的父 `eventsEl` 容器時 |
stretchY | number | 0 | 在此值上沿 Y 軸移動(平移)atropos 容器(以百分比表示)。例如,`50` 值將移動容器高度的 +-50%。僅適用於多個 Atropos,當多個 Atropos 使用相同的父 `eventsEl` 容器時 |
stretchZ | number | 0 | 在此值上沿 Z 軸移動(平移)atropos 容器(以百分比表示)。例如,`50` 值將移動容器高度的 +-50%。僅適用於多個 Atropos,當多個 Atropos 使用相同的父 `eventsEl` 容器時 |
commonOrigin | boolean | true | 將動態地將具有相同父級的所有 Atropos 組件的 `transform-origin` 設置為同一點(通常是指標位置)。僅適用於多個 Atropos,當多個 Atropos 使用相同的父 `eventsEl` 容器時 |
shadow | boolean | true | 啟用陰影 |
shadowOffset | number | 50 | 陰影偏移量,基本上是陰影元素的 translateZ 值 |
shadowScale | number | 1 | 陰影比例因子 |
highlight | boolean | true | 啟用高亮 |
onEnter | function | 回調函數Atropos 激活時(懸停時)將觸發 | |
onLeave | function | 回調函數Atropos 取消激活時(指標移出時)將觸發 | |
onRotate | function(x, y) | 回調函數旋轉時將觸發。作為參數接受 x 和 y 旋轉角度 |
例如
const myAtropos = Atropos({el: '.my-atropos',activeOffset: 40,shadowScale: 1.05,onEnter() {console.log('Enter');},onLeave() {console.log('Leave');},onRotate(x, y) {console.log('Rotate', x, y);}});
初始化 Atropos 後,我們將獲得其初始化的實例,其中包含一些有用的方法和屬性
名稱 | 類型 | 描述 |
---|---|---|
el | HTMLElement | 主容器 HTML 元素 |
isActive | boolean | 指示 Atropos 是否處於活動狀態(懸停) |
params | object | 用於初始化的 Atropos 參數(使用默認值擴展) |
destroy() | function | 銷毀 Atropos 並分離所有事件偵聽器的方法 |
destroyed | boolean | 指示 Atropos 是否已被銷毀 |
例如
// init Atroposconst myAtropos = Atropos({el: '.my-atropos',activeOffset: 40,});setTimeout(() => {// destroy it when neededmyAtropos.destroy();}, 10000)
Atropos 使用特殊的 data-
屬性來控制元素偏移(視差效果)
data-atropos-offset
- 以百分比控制元素偏移/平移。
data-atropos-offset="5"
表示元素在最大旋轉角度下將移動其大小的 5%
。data-atropos-offset="-5"
表示元素在最大旋轉角度下將移動其大小的 -5%
。基本上,如果您希望元素看起來更近(位於場景的前面),則此值應為正值。如果您希望元素出現在遠處(位於場景的後面),則此值應為負值。
data-atropos-opacity
- 控制元素的不透明度。data-atropos-opacity="0;1"
- 表示元素的不透明度將從 0
(初始狀態)更改為 1
(最大旋轉角度)。data-atropos-opacity="0.1;0.8"
- 表示元素的不透明度將從 0.1
(初始狀態)更改為 0.8
(最大旋轉角度)。data-atropos-opacity="1;0"
- 表示元素的不透明度將從 1
(初始狀態)更改為 0
(最大旋轉角度)。例如
<div class="atropos"><div class="atropos-scale"><div class="atropos-rotate"><div class="atropos-inner"><!--Element with negative offset will move in reverse direction,making it appear behind the scene--><img src="image-bg.png" data-atropos-offset="-5" /><!--Element with no offset will not move--><img src="image-middle.png" data-atropos-offset="0" /><!--Element with positive offset will move in same direction,making it appear in front of the scene--><img src="image-front.png" data-atropos-offset="5" /></div></div></div></div>
如您所見,將 Atropos 集成到您的網站或應用程序中很容易。所以這裡是您的下一步