我有一个奇怪的问题。我已经挖掘了这段代码片段,它允许文本行向左动画,然后无限重复。虽然这很完美。我希望在所有行上加载动画,但速度要慢得多。然后当鼠标悬停在每一行上时,动画会加快速度。
https://codepen.io/umbriel/pen/RjYqRR
到目前为止,我已经尝试过循环每个问题,然后运行动画。但它仍然只抓住其中一个div并激活它。
var el_text = questions[i].querySelectorAll('.intro-question-text');
for (var i = 0; i < el_text.length; i++) {
el_text[i].style.transform = 'translate3d(' + qScrollPositions[i] + 'px,0,0)'
}
任何指针?非常感谢你!
答案 0 :(得分:1)
您可以使用setInterval函数(当用户将鼠标悬停在元素上时将其交换为更快的函数)来使其执行您想要的操作。
var textCopy = document.querySelectorAll('.intro-question-text')
for (var i = 0; i < textCopy.length; i++) {
var text = textCopy[i].innerHTML.repeat(5)
textCopy[i].insertAdjacentHTML('beforeend', text)
}
var WIN_H,
WIN_W,
qFrame = [];
var questions = document.querySelectorAll('.intro-question');
var scrollRequest;
var qScrollPositions = [];
var passiveRepeaters = [];
var repeater;
function initIntroScript() {
Array.prototype.map.call(questions, function(q, i) {
addImageHover(i);
})
window.addEventListener('resize', resizeHandler);
resizeHandler();
}
function addImageHover(i) {
qFrame[i] = 0;
qScrollPositions[i] = Math.floor(Math.random() * -200);
questions[i].querySelector('.intro-question-text').style.transform = 'translate3d(' + qScrollPositions[i] + 'px,0,0)';
questions[i].addEventListener('mouseenter', function() {
//console.log("mouseenter -> " + i);
clearInterval(passiveRepeaters[i]);
repeater = setInterval(function() {
scrollQuestionText(i);
}, 10);
});
questions[i].addEventListener('mouseleave', function() {
//console.log("mouseleave -> " + i);
clearInterval(repeater);
passiveRepeaters[i] = setInterval(function() {
scrollQuestionText(i);
}, 20);
});
passiveRepeaters[i] = setInterval(function() {
scrollQuestionText(i);
}, 20);
}
function scrollQuestionText(i) {
var shift = Math.floor(4 + WIN_W/800) * Math.min(0.2, qFrame[i] / 20);
cancelAnimationFrame(scrollRequest);
var el_text = questions[i].querySelector('.intro-question-text');
qScrollPositions[i] = qScrollPositions[i] - shift;
if (qScrollPositions[i] < -el_text.clientWidth / 2 - 5) {
qScrollPositions[i] = 0;
}
el_text.style.transform = 'translate3d(' + qScrollPositions[i] + 'px,0,0)'
qFrame[i]++;
}
initIntroScript();
function resizeHandler() { // NEEDS TO NOT BREAK ON RESIZE
WIN_W = window.innerWidth;
WIN_H = window.innerHeight;
}
body {
font-family: sans-serif;
font-size:4rem;
}
.intro-question {
position: relative;
color: #000000;
cursor: pointer;
transition: .3s;
white-space: nowrap;
padding: 0;
backface-visibility: hidden;
will-change: transform, color, background;
flex-shrink: 2;
/* opacity: 0; */
transition: opacity .5s, transform 1s ease-out;
z-index: 2;
}
.intro-question-text {
backface-visibility: hidden;
will-change: transform;
position: relative;
display: inline-block;
transition: .5s opacity;
}
<div class="intro-questions">
<div class="intro-question">
<div class="intro-question-text white">
Testing content in here
</div>
</div>
<div class="intro-question">
<div class="intro-question-text white">
Content testing
</div>
</div>
<div class="intro-question">
<div class="intro-question-text white">
Content stackoverflow please
</div>
</div>
</div>
修改强>
您可以通过缩短函数调用之间的间隔来增加帧速率,例如
questions[i].addEventListener('mouseenter', function() {
//console.log("mouseenter -> " + i);
clearInterval(passiveRepeaters[i]);
repeater = setInterval(function() {
scrollQuestionText(i);
}, 10);
});
questions[i].addEventListener('mouseleave', function() {
//console.log("mouseleave -> " + i);
clearInterval(repeater);
passiveRepeaters[i] = setInterval(function() {
scrollQuestionText(i);
}, 20);
});
passiveRepeaters[i] = setInterval(function() {
scrollQuestionText(i);
}, 20);
如果您这样做并且不希望滚动速度加快,则可以像这样减小步长
var shift = Math.floor(4 + WIN_W/800) * Math.min(0.2, qFrame[i] / 10);