gpt4 book ai didi

javascript - 滚动时尝试动画​​和元素

转载 作者:行者123 更新时间:2023-11-30 21:11:12 24 4
gpt4 key购买 nike

我正在尝试使 Logo 向右滚动并最终动画化为汉堡包图标。这大致是我的最终目标,但在页面加载而不是滚动时完成。 https://codepen.io/mthauv/pen/gxJNWq

我首先尝试让 Logo 位于正确的位置。我遇到的问题是它只有在您仔细滚动时才有效。如果您滚动得太快,它要么滚动得太远,要么滚动得不够远,具体取决于行...

 if( target - mContainer.getBoundingClientRect().right > 0 || startLine - startLine * scrollY/240 > 16 ){
mContainer.style.right = startLine - startLine * scrollY/240 + "px";
check = mContainer.style.right.replace(/[^\d.-]/g, '');
}

Or 语句允许它滚动太远而 and 语句允许它短暂停止。我尝试了其他一些陈述,但大多数都阻止了向下滚动时返回 Logo 。

这是笔 https://codepen.io/mthauv/pen/aygNbe

HTML

 <body>

<div id="logo-container" class="first-step">
<div id="m-container" onclick="toggleMenu('menu-btn')">
<div id="m-1" class="m"></div>
<div id="m-2" class="m"></div>
<div id="m-3" class="m"></div>
</div>
</div>

<div id="menu-container">
<ul id="menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>

<div id="content-container" onclick="toggleMenu('body')">
<h1>Content</h1>
</div>

</body>

和JS

var logoContainer = document.getElementById('logo-container');
var mContainer = document.getElementById('m-container');
var menuContainer = document.getElementById('menu-container');
var contentContainer = document.getElementById('content-container');
var m = document.getElementsByClassName('m');

var w = window.innerWidth;
var check = mContainer.style.right.replace(/[^\d.-]/g, '');
var startLine = w/2 - 90;
var totalDistance = window.innerWidth/2;
var target = window.innerWidth - 16;

mContainer.style.right = w/2 - 90 + "px"

window.addEventListener("resize", function(){
var w = window.innerWidth;
mContainer.style.right = w/2 - 90 + "px"
check = mContainer.style.right.replace(/[^\d.-]/g, '');
startLine = w/2 - 90;
totalDistance = w/2;
target = w - 16;
});

window.addEventListener('scroll', function(e){
var scrollY = (window.pageYOffset) ;
var x = scrollY/240;

if( target - mContainer.getBoundingClientRect().right > 0 || startLine - startLine * scrollY/240 > 16 )
{
mContainer.style.right = startLine - startLine * scrollY/240 + "px";
check = mContainer.style.right.replace(/[^\d.-]/g, '');
}

});

最佳答案

检查一下:

const mContainer = document.getElementById("m-container");

const scrollFinishPosition = 300; // when animation should be finished
const step = scrollFinishPosition / 100;

let startPosition;
let endPosition;
let difference;

function calcSize() {
const logoWidth = mContainer.offsetWidth; // get width of the logo

startPosition = window.innerWidth / 2 - logoWidth / 2; // center

const margin = 80;
endPosition = window.innerWidth - logoWidth - margin;

// used for some simple math later
difference = endPosition - startPosition;
}

function positionLogo() {
let percentageValue;

// if scroll position is in animation range - calc % otherwise just use 100%
if (window.pageYOffset < scrollFinishPosition) {
percentageValue = pageYOffset / step;
} else {
percentageValue = 100;
}

// convert % to px and add update styles
const position = startPosition + difference / 100 * percentageValue;
mContainer.style.left = position + "px";
}

window.addEventListener("resize", function(e) {
calcSize();
positionLogo()
});

window.addEventListener("scroll", function(e) {
positionLogo();
});

calcSize();
positionLogo();

https://codepen.io/Gibala/pen/gxNJRR

关于javascript - 滚动时尝试动画​​和元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46119687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com