简体   繁体   English

需要帮助修改 skew.js 以进行 Y 轴旋转

[英]Need help modifying skew.js for Y axis rotation

Here's the site in question: https://marks-groovy-project-2fa056.webflow.io/这是有问题的网站: https : //marks-groovy-project-2fa056.webflow.io/

I want to rotate each letter by 90 degrees on their Y axis while scroll is active, and return them back to their original position as soon as scrolling stops.我想在滚动处于活动状态时将每个字母在其 Y 轴上旋转 90 度,并在滚动停止后立即将它们返回到原始位置。

Visualization: in top view, a letter should rotate 90deg counter-clockwise when scroll is activated, which will render the letter invisible in front view ( the viewport) while scrolling, and then turn back 90 degrees clockwise when scrolling ends, so that each letter is visible again.可视化:在顶视图中,激活滚动时字母应逆时针旋转 90 度,这将使字母在滚动时在前视图(视口)中不可见,然后在滚动结束时顺时针旋转 90 度,以便每个字母再次可见。

Method: used skew.js and slightly modified it:方法:使用skew.js,稍微修改一下:

  1. skew.js is applied to an entire section. skew.js 应用于整个部分。 I want to apply it to every instance of a span with id="letter-animation".我想将它应用于 id="letter-animation" 的每个跨度实例。 I've appropriately renamed the constant and referenced the #.我已经适当地重命名了常量并引用了#。

  2. const speed is a remnant from skew.js. const speed 是 skew.js 的残余。 I haven't yet figured out how to rewrite it.我还没有想出如何重写它。 It expresses the amount of skew as a function of the difference in newPixel/oldPixel.它将偏斜量表示为 newPixel/oldPixel 差异的函数。 Which I don't want.我不想要的。 My rotation needs to be 90deg every time.我的旋转每次都需要 90 度。 Once in, once out.一进一出。

  3. letter.style.transform = "rotateY(45deg)" used to be "rotateY(" + speed + "deg)" in the old script. letter.style.transform = "rotateY(45deg)" 在旧脚本中曾经是 "rotateY(" + speed + "deg)"。 (technically it was "skewY", not "rotateY" but you get my point). (从技术上讲,它是“skewY”,而不是“rotateY”,但您明白我的意思)。 const speed would then be replaced with whatever new constant is appropriate as mentioned in point 2. const speed 将被替换为第 2 点中提到的任何合适的新常量。

I've set up this codepen to isolate the script in question.我已经设置了这个 codepen 来隔离有问题的脚本。 https://codepen.io/mhedinger/pen/yLJaLmp https://codepen.io/mhedinger/pen/yLJaLmp

const letter = document.querySelector("#letter-animation")
let currentPixel = window.pageYOffset

const looper = function(){
const newPixel = window.pageYOffset
const diff = newPixel - currentPixel
const speed = diff * 11

letter.style.transform = "rotateY(45deg)"

currentPixel = newPixel

requestAnimationFrame(looper)
}

looper()

Here's the tutorial that explains how skew.js works: https://www.superhi.com/video/skew-on-scroll-effect这是解释 skew.js 如何工作的教程: https ://www.superhi.com/video/skew-on-scroll-effect

And here's an example of it working, according to the video tutorial above: https://codepen.io/emgiust/pen/rdOJwQ根据上面的视频教程,这是一个工作示例: https : //codepen.io/emgiust/pen/rdOJwQ

const section = document.querySelector("section");
let currentPixel = window.pageYOffset

//looper keeps running and keeps track of where the new pixel is
const looper = function () {
const newPixel = window.pageYOffset;
const diff = newPixel - currentPixel
const speed = diff * 0.35;

section.style.transform = "skewY(" + speed + "deg)"


currentPixel = newPixel;

requestAnimationFrame(looper)
}

looper();

Can anybody help me get this working?有人可以帮我解决这个问题吗? So far, the main issue seems to be that they're spans and not sections, but it could also just be my complete absence of understanding of javascript.到目前为止,主要问题似乎是它们是跨度而不是部分,但这也可能只是我完全不了解 javascript。

I'm hoping to control the transition speed, curve, and delay via css, but if it has to be controlled in JS, i'd also appreciate some advice on how to do this.我希望通过 css 控制过渡速度、曲线和延迟,但是如果必须在 JS 中控制它,我也很感激有关如何做到这一点的一些建议。

Thank you everyone in advance for trying to help.预先感谢大家的帮助。

Cheers, Mark干杯,马克

NOTE: there is another script running (fullpage.js) which simulates a swiping experience during scroll.注意:还有另一个脚本正在运行 (fullpage.js),它模拟滚动期间的滑动体验。 deactivate it temporarily to get a better view of what's happening during scroll while you're checking things out/setting things up.暂时停用它,以便在您检查/设置时更好地了解滚动期间发生的情况。 End-product, a letter animation that synchronously rotates each letter by 90 degrees (rendering the words invisible) for the duration of the swipe/scroll, and returning them to normal once the section snaps into place.最终产品,一个字母动画,在滑动/滚动期间同步旋转每个字母 90 度(使单词不可见),并在部分卡入到位后将它们恢复正常。

In case anybody is still interested in this, I have figured it out in the meantime.如果有人仍然对此感兴趣,我在此期间已经弄清楚了。

const letter = document.getElementsByTagName("SPAN");
var timer = null;

window.addEventListener('scroll', function() {
    if(timer !== null) {clearTimeout(timer)}
    timer = setTimeout(function() {
      var i;
      for (i = 0; i < letter.length; i++) {
          letter[i].style.transform = "rotateY(0deg)";
          };
      }, 150);
  
    var i;
    for (i = 0; i < letter.length; i++) {
      letter[i].style.transform = "rotateY(90deg)";
      };
    }, false);

https://codepen.io/mhedinger/pen/yLJaLmp https://codepen.io/mhedinger/pen/yLJaLmp

EDIT:编辑:

I have improved this script for best-practice purposes.为了最佳实践的目的,我改进了这个脚本。 This makes it more stable and versatile because it can now be used together with plugins like fullpage.js.这使得它更加稳定和通用,因为它现在可以与 fullpage.js 等插件一起使用。 Additionally, if desired, the transforms can now also be called manually by referencing the appropriate function.此外,如果需要,现在还可以通过引用适当的函数手动调用转换。

let letter = document.getElementsByTagName("SPAN");
var timer = null;

var rotateLetter = function() {    
    Array.from(letter).forEach(function(letter) {
    letter.style.transform = "rotateY(90deg)";
    });
    };
var resetLetter = function() {    
    Array.from(letter).forEach(function(letter) {
    letter.style.transform = "rotateY(0deg)";
    });
    };

window.addEventListener('scroll', function() {
    
    if(timer !== null) {clearTimeout(timer)}
    
    timer = setTimeout(function() {resetLetter()}, 125);
        rotateLetter()
        }, false);

https://codepen.io/mhedinger/pen/dypmKZd https://codepen.io/mhedinger/pen/dypmKZd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM