跟随鼠标3D倾斜

这篇文章详细介绍了VanillaTilt.js库,一个用于创建动态倾斜效果的轻量级JavaScript类,支持鼠标事件、设备方向感应器(gyroscope)以及自定义设置。它允许用户控制元素的倾斜角度、速度、眩光效果等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 创建一个vanilla-tilt.js文件
  2. 将一下代码黏贴进去
  3. export var VanillaTilt = (function () {
      'use strict';
    
      /**
       * Created by Sergiu Șandor (micku7zu) on 1/27/2017.
       * Original idea: https://github.com/gijsroge/tilt.js
       * MIT License.
       * Version 1.7.2
       */
    
      class VanillaTilt {
        constructor(element, settings = {}) {
          if (!(element instanceof Node)) {
            throw ("Can't initialize VanillaTilt because " + element + " is not a Node.");
          }
    
          this.width = null;
          this.height = null;
          this.clientWidth = null;
          this.clientHeight = null;
          this.left = null;
          this.top = null;
    
          // for Gyroscope sampling
          this.gammazero = null;
          this.betazero = null;
          this.lastgammazero = null;
          this.lastbetazero = null;
    
          this.transitionTimeout = null;
          this.updateCall = null;
          this.event = null;
    
          this.updateBind = this.update.bind(this);
          this.resetBind = this.reset.bind(this);
    
          this.element = element;
          this.settings = this.extendSettings(settings);
    
          this.reverse = this.settings.reverse ? -1 : 1;
          this.glare = VanillaTilt.isSettingTrue(this.settings.glare);
          this.glarePrerender = VanillaTilt.isSettingTrue(this.settings["glare-prerender"]);
          this.fullPageListening = VanillaTilt.isSettingTrue(this.settings["full-page-listening"]);
          this.gyroscope = VanillaTilt.isSettingTrue(this.settings.gyroscope);
          this.gyroscopeSamples = this.settings.gyroscopeSamples;
    
          this.elementListener = this.getElementListener();
    
          if (this.glare) {
            this.prepareGlare();
          }
    
          if (this.fullPageListening) {
            this.updateClientSize();
          }
    
          this.addEventListeners();
          this.reset();
          this.updateInitialPosition();
        }
    
        static isSettingTrue (setting) {
          return setting === "" || setting === true || setting === 1;
        }
    
        /**
         * Method returns element what will be listen mouse events
         * @return {Node}
         */
        getElementListener () {
          if (this.fullPageListening) {
            return window.document;
          }
    
          if (typeof this.settings["mouse-event-element"] === "string") {
            const mouseEventElement = document.querySelector(this.settings["mouse-event-element"]);
    
            if (mouseEventElement) {
              return mouseEventElement;
            }
          }
    
          if (this.settings["mouse-event-element"] instanceof Node) {
            return this.settings["mouse-event-element"];
          }
    
          return this.element;
        }
    
        /**
         * Method set listen methods for this.elementListener
         * @return {Node}
         */
        addEventListeners () {
          this.onMouseEnterBind = this.onMouseEnter.bind(this);
          this.onMouseMoveBind = this.onMouseMove.bind(this);
          this.onMouseLeaveBind = this.onMouseLeave.bind(this);
          this.onWindowResizeBind = this.onWindowResize.bind(this);
          this.onDeviceOrientationBind = this.onDeviceOrientation.bind(this);
    
          this.elementListener.addEventListener("mouseenter", this.onMouseEnterBind);
          this.elementListener.addEventListener("mouseleave", this.onMouseLeaveBind);
          this.elementListener.addEventListener("mousemove", this.onMouseMoveBind);
    
          if (this.glare || this.fullPageListening) {
            window.addEventListener("resize", this.onWindowResizeBind);
          }
    
          if (this.gyroscope) {
            wind
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值