我想在 div 滚动到视口中时启动一个函数。我的问题是,每次我继续滚动时,该功能都会再次触发/启动。
HTML:
<div class="box"></div>
Run Code Online (Sandbox Code Playgroud)
JS:
$(document).ready(function() {
function start() {
alert("hello");
}
$(window).scroll(function() {
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
start();
} else {
$(".box").removeClass("green");
}
});
});
Run Code Online (Sandbox Code Playgroud)
总结一下:当 div 滚动到视口中时,应该启动“start”函数。但触发一次后不应再次触发。
我读过一些关于从中心缩放 SVG 路径的文章,但没有任何作用。
div { width: 100px; height: 100px; background: rgba(0,0,0,0.2)}
#circle {
transform: scale(1) translate(0px, 0px);
animation: circle .5s linear 1.3s forwards;
}
@keyframes circle {
to {
transform: scale(4.1) translate(-38px, -38px);
}
}Run Code Online (Sandbox Code Playgroud)
<div>
<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="circle" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<circle id="Oval" fill="#2A2C32" cx="50" cy="50" r="11.5"></circle>
</g>
</svg>
</div>Run Code Online (Sandbox Code Playgroud)