我希望某些div在Javascript中在屏幕上移动,有点像本网站 (向下滚动),只在水平方向移动,或者像这里 (也是垂直的,但我希望它是水平的)。
以下是我的HTML:
<div id = "creators" class = "big-part">
<h3>Creators</h3>
<div class = "creator_name">
<h4>Name</h4>
<p>Text</p>
</div>
<div class = "creator_name">
<h4>Name</h4>
<p>Text</p>
</div>
<div class = "creator_name">
<h4>Name</h4>
<p>Text</p>
</div>
</div>我的CSS:
.big-part {
color: rgb(230, 230, 230);
background-color: #3c3c91;
border-radius: 21px;
padding: 0.5% 1.5%;
margin-top: 2%;
}
.creator_name {
background-color: rgb(230, 230, 230);
height: 300px;
color: #3c3c91;
border-radius: 15px;
padding: 1%;
}
.creator_name {
width: 25%;
display: inline-block;
margin: 2% 2%;
}以及位于script标记中的javascript:
var creatorEl= document.getElementsByClassName("creator_name");
var startTime = new Date().getTime();
var moveTheDiv = function() {
var currTime = new Date().getTime();
var secondsElapsed = ((currTime - startTime)/1000);
var newLeft = parseFloat(secondsElapsed) * 30 + "px";
creatorEl.style.left = newLeft;
window.requestAnimationFrame(walkTheCat);
};
moveTheDiv();谢谢!
顺便说一句,只有当客户机/用户向下滚动并且div是可见的时,这个动画才会发生。你可以回答一半的问题,如果你想,我会接受任何帮助。
编辑:只有在向每个ID中添加一个ID并使用ID移动它们时,才能使它们移动,是否有一种方法可以同时移动它们,但只使用类?
发布于 2015-12-29 00:15:58
您可能会发现动画引擎Velocity.js很有用。http://julian.com/research/velocity/ (我与创建者无关)。
您实现的函数类似于jQuery的.animate()方法。
下面是如何旋转div的示例:
$(document).ready(function(){
$('#divToAnimate').velocity(
{ rotateX: "+=0",
rotateY: "+=360",
height: '+=50px',
width: '+=100px'
},
{
duration:4000,
loop:false,
easing:'linear'
});
});<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample animation with Velocity.js</title>
<!-- jQuery CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<!-- Velocity.js CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>
</head>
<body>
<div id="divToAnimate">Example Div to Animate</div>
</body>
</html>
希望你能乐于助人,好运!
https://stackoverflow.com/questions/34424821
复制相似问题