JS设置css值 XXX.style.xxx 获取css值 XXX.offsetxxx
比如 oDiv.style.left = oDiv.offsetLeft + 10 + 'px';
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cartoon</title>
<style type="text/css">
body, div, span {
margin: 0;
padding: 0;
}
#div1 {
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span {
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
}
</style>
</head>
<body>
<script>
window.onload = function() {
var oDiv = document.getElementById("div1");
oDiv.onmouseover = function() {
move(0);
}
oDiv.onmouseout = function() {
move(-200);
}
}
var timer = null;
function move(dis) {
clearInterval(timer);
var oDiv = document.getElementById("div1");
timer = setInterval(function() {
var speed = 0;
if(oDiv.offsetLeft > dis){
speed = -10;
}
else {
speed = 10;
}
if(oDiv.offsetLeft == dis) {
clearInterval(timer);
}
else {
oDiv.style.left = oDiv.offsetLeft + speed + 'px';
}
},30)
}
</script>
<div id="div1">
<span id="share">分享</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#div1{
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span{
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top:75px;
}
</style>
</head>
<body>
<div id="div1">
<span id="share">xc</span>
</div>
</body>
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove(10,0);
}
oDiv.onmouseout=function(){
startMove1(10,-200);
}
}
var timer=null;
function startMove(sleep,taiger){
clearInterval(timer)
var oDiv=document.getElementById('div1');
timer=setInterval(function(){
if(oDiv.offsetLeft==taiger){
clearInterval(timer);
}
else{
oDiv.style.left=oDiv.offsetLeft+sleep+'px';
}
},30);
}
function startMove1(sleep,taiger){
clearInterval(timer)
var oDiv=document.getElementById('div1');
timer=setInterval(function(){
if(oDiv.offsetLeft<= taiger){
clearInterval(timer);
}
else{
oDiv.style.left=oDiv.offsetLeft-sleep+'px';
}
},30);
}
</script>
</html>
用offsetLeft是因为left可读写,且offset返回的是数值,left返回的是字符串
setInterval(函数,毫秒) 定时器
onmouseover 鼠标移入
onmouseout 鼠标移出
object.style.left 修改left
object.offsetLeft 当前的left
object.style.left=object.offsetLeft+num'px'
clearInterval 清除定时器
记得一开始执行函数的时候应该清空定时器
setInterval(函数,毫秒);定时器
ele.style.left 修改left
ele.offsetLeft 当前的left
ele.style.left = ele.offsetLeft + n + 'px' 记得加px
timer = 定时器
clearInterval(timer);清空定时器
记得一开始执行函数的时候应该清空定时器
onmouseover 鼠标移入
onmouseout鼠标移除
position:relative;指相对于自己原本的位置偏移;position:absolute;指相对于父元素的定位
<span></span>是行内元素,要添加position或者display才能设置宽度和高度
设置圆角半径border-radius
元素运动的单位距离跟运动范围长度相关,距离为非整数时,单位距离设置成整数可能出现问题,要注意调整
setInterval(函数,毫秒);定时器
ele.style.left 修改left
ele.offsetLeft 当前的left
ele.style.left = ele.offsetLeft + n + 'px' 记得加px
timer = 定时器
clearInterval(timer);清空定时器
记得一开始执行函数的时候应该清空定时器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS速度动画</title> <style> *{ padding:0; margin: 0; } .shareWrap{ width: 200px; height: 150px; background: burlywood; margin-top: 50px; position: relative; left: -200px; } .shareWrap .share{ display: inline-block; width: 22px; background: aquamarine; padding: 2px 4px; position: absolute; top: 0; right: -30px; } </style> </head> <body> <div class="shareWrap" id="shareWrap"> <span class="share">分享</span> </div> <script> window.onload = function() { var oDiv = document.getElementById('shareWrap'); var timer = null; // 鼠标移入按钮动画 oDiv.onmouseover = function(){ startMove(0); }; function startMove(iTarget){ clearInterval(timer); timer = setInterval(function(){ var speed = 0; if (oDiv.offsetLeft > iTarget){ // 当左侧偏移量大于目标位置时,那么速度是向左移动,为负值, 否则为正值 speed = -10; }else{ speed = 10; } if (oDiv.offsetLeft === iTarget){ //当元素移动到目标位置时,清除定时器停止移动 clearInterval(timer); }else{ oDiv.style.left = oDiv.offsetLeft + speed + 'px'; } },30); } // 鼠标移出按钮动画 oDiv.onmouseout = function(){ startMove(-200); } } </script> </body> </html>
代码截图
定时器
onmouseover 鼠标移入
onmouseout鼠标移除
odiv.offsetLeft odiv的当前left值
在两个代码很相似的情况下,可以把不同的地方挑出来作为参数传进去(如鼠标移入与鼠标移除的函数)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分享</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#div1{
width: 200px;
height: 200px;
background-color: red;
position: relative;
left: -200px;
top: 0;
}
#div1 span{
width: 20px;
height: 50px;
background-color: blue;
position:absolute;
left: 200px;
top: 75px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");//oDiv是局部变量,只在此函数内有效
//鼠标移入事件
oDiv.onmouseover=function(){
starMove(0);
}
//鼠标移出事件
oDiv.onmouseout=function(){
starMove(-200);
}
var timer = null;
function starMove(target){
clearInterval(timer);//解决“speed”的叠加问题
var oDiv=document.getElementById("div1");
//创建一个定时器timer
timer=setInterval(function(){
var speed =0;
if(oDiv.offsetLeft > target){
speed = -10;
}else{
speed = 10;
}
if(oDiv.offsetLeft==target){
clearIntral(timer);//符合条件运动停止
}else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
}
</script>
</head>
<body>
<div id = div1>
<span>分享</span>
</div>
</body>
</html>
offsetLeft:当前值