刚学js,做了个动画,有点问题,有大神帮忙看下? <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>absolute</title>
<!-- styles of the box styles of the boxstyles of the box -->
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 2000px;
}
/*//big box */
#container{
width: 200px;
height: 400px;
background: #ccc;
position: relative;
top: 200px;
left:-200px;
}
/* small box small boxsmall boxsmall box*/
#tip{
width: 60px;
height: 400px;
background: #f00;
text-align: center;
line-height: 400px;
position: absolute;
left: 200px;
top: 0;
}
</style>
</head>
<body>
<!-- html box html box html box html box -->
<div id="container">
<div id="tip">contact</div>
</div>
<script>
var ocontainer=document.getElementById("container");
//mouseover programme mouseover programmemouseover programme
ocontainer.onmouseover=function(){
//make a new object
var status={object:ocontainer,attr:{left:0}};
animation(status);
}
//mouseout programme mouseout programmemouseout programme
ocontainer.onmouseout=function(){
//make a new object
var status1={object:ocontainer,attr:{left:-200}};
//delay function after mouse moveout
setTimeout(()=>{
console.log("here")
timeoutP(status1)
},2000)
//function of the timeout
function timeoutP(status1){
animation(status1);
}
};
//animation function animation function animation function
function animation(status){
var object=status.object;
// console.log(object)
// console.log(status.attr)
clearInterval(object.timer)
// console.log(attrValueNow);
object.timer=setInterval(() => {
console.log(object.timer)
timerProgram(object,status)
}, 50);
//change every attribute change every attribute change every
function timerProgram(object,status){
// console.log(object,i)
var flag=true;
for (var i in status.attr) {
var attrValueNow=status.attr[i];
var attrValueBefore=parseInt(getStyleValue(object,i));
// console.log(attrValueBefore)
var step;
if (attrValueNow-attrValueBefore<0) {
step=Math.floor((attrValueNow-attrValueBefore)/10)
}else{
step=Math.ceil((attrValueNow-attrValueBefore)/10);
}
var absoluteStep=step<0?-step:step;
// console.log(step,attrValueBefore)
var temp=attrValueBefore+step;
// console.log(temp);
// some special attributes
if (i=="offsetLeft"||i=="offsetTop") {
object[i]=temp+"px";
// console.log(object[i])
}else{
object.style[i]=temp+"px";
}
var tt=attrValueNow-temp;
//the last step
if (Math.abs(tt)<=absoluteStep) {
object.style[i]=attrValueNow+"px";
}else{
flag=false;
}
}
//off the loop
if (flag) {
clearInterval(object.timer)
}
}
//get box styles
function getStyleValue(obj,attr){
// console.log(obj)
if (attr=="offsetLeft"||attr=="offsetTop") {
// console.log(obj[attr])
return obj[attr];
}else{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,null)[attr]
}
}
}
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>absolute</title>
<!-- styles of the box styles of the boxstyles of the box -->
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 2000px;
}
/*//big box */
#container{
width: 200px;
height: 400px;
background: #ccc;
position: relative;
top: 200px;
left:-200px;
}
/* small box small boxsmall boxsmall box*/
#tip{
width: 60px;
height: 400px;
background: #f00;
text-align: center;
line-height: 400px;
position: absolute;
left: 200px;
top: 0;
}
</style>
</head>
<body>
<!-- html box html box html box html box -->
<div id="container">
<div id="tip">contact</div>
</div>
<script>
var ocontainer=document.getElementById("container");
//mouseover programme mouseover programmemouseover programme
ocontainer.onmouseover=function(){
//make a new object
var status={object:ocontainer,attr:{left:0}};
animation(status);
}
//mouseout programme mouseout programmemouseout programme
ocontainer.onmouseout=function(){
//make a new object
var status1={object:ocontainer,attr:{left:-200}};
//delay function after mouse moveout
setTimeout(()=>{
console.log("here")
timeoutP(status1)
},2000)
//function of the timeout
function timeoutP(status1){
animation(status1);
}
};
//animation function animation function animation function
function animation(status){
var object=status.object;
// console.log(object)
// console.log(status.attr)
clearInterval(object.timer)
// console.log(attrValueNow);
object.timer=setInterval(() => {
console.log(object.timer)
timerProgram(object,status)
}, 50);
//change every attribute change every attribute change every
function timerProgram(object,status){
// console.log(object,i)
var flag=true;
for (var i in status.attr) {
var attrValueNow=status.attr[i];
var attrValueBefore=parseInt(getStyleValue(object,i));
// console.log(attrValueBefore)
var step;
if (attrValueNow-attrValueBefore<0) {
step=Math.floor((attrValueNow-attrValueBefore)/10)
}else{
step=Math.ceil((attrValueNow-attrValueBefore)/10);
}
var absoluteStep=step<0?-step:step;
// console.log(step,attrValueBefore)
var temp=attrValueBefore+step;
// console.log(temp);
// some special attributes
if (i=="offsetLeft"||i=="offsetTop") {
object[i]=temp+"px";
// console.log(object[i])
}else{
object.style[i]=temp+"px";
}
var tt=attrValueNow-temp;
//the last step
if (Math.abs(tt)<=absoluteStep) {
object.style[i]=attrValueNow+"px";
}else{
flag=false;
}
}
//off the loop
if (flag) {
clearInterval(object.timer)
}
}
//get box styles
function getStyleValue(obj,attr){
// console.log(obj)
if (attr=="offsetLeft"||attr=="offsetTop") {
// console.log(obj[attr])
return obj[attr];
}else{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,null)[attr]
}
}
}
}
</script>
</body>
</html>
解决方案 »
- 鼠标移动到<P>标记时,可以复制P标记中的内容
- google map的缩放级别问题
- javascript生成图片
- 有没有考虑过:在浏览器中用javascript实现一个可视化客户端DOM UI开发工具?
- 在CSS中定义一个DIV的position为absolute,但用脚本o.style.position读不到???
- 一个firefox与IE兼容的问题,点击图片显示div
- 急,怎样在object标签上用DIV做弹出层
- 急 !!!!!!!!!!!!如何用JavaScript画甘特图.
- 如何控制在文本框里的输入
- radio非要放在form里才行吗
- 跪求!WEB容器 安装与下载
- 请教一下webpack的相关问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>absolute</title>
<!-- styles of the box styles of the boxstyles of the box -->
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 2000px;
}
/*//big box */
#container {
width: 200px;
height: 400px;
background: #ccc;
position: relative;
top: 200px;
left: -200px;
transition: left 0.3s ease-in-out; }
/* small box small boxsmall boxsmall box*/
#tip {
width: 60px;
height: 400px;
background: #f00;
text-align: center;
line-height: 400px;
position: absolute;
left: 200px;
top: 0;
}
</style>
</head>
<body>
<!-- html box html box html box html box -->
<div id="container">
<div id="tip">contact</div>
</div>
<script>
var ocontainer = document.getElementById("container");
//mouseover programme mouseover programmemouseover programme
ocontainer.onmouseover = function () {
this.style.left = 0
}
//mouseout programme mouseout programmemouseout programme
ocontainer.onmouseout = function () {
this.style.left = "-200px"
};
</script>
</body>
</html>
问题找到了,mouseover 换成mouseenter就好了,这两有点区别,,,,