6

我试图在显示时将 div 向左滑动,在隐藏时将其向右滑动,但我不想使用 jQuery。有没有办法在不使用 javascript 库的情况下制作简单的动画并支持 IE7 和 IE8?

这是我的显示/隐藏 js:

function showHide() {
 var Elliot = document.getElementById('Daniel').style.display;

 if (Elliot == "block") {
  document.getElementById('Daniel').style.display = "none"; 
 } else {
  document.getElementById('Daniel').style.display = "block";
 };
};

HTML 看起来像:

<a href="#" onclick="showHide();return false;">click me</a>

<div id="Daniel" style="display:block; width: 300px; height: 50px;">
<!-- some stuff -->
</div>
4

1 回答 1

10

以下是一个可以帮助您入门的功能:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style>
#yea {
    margin-left: 100px;
    height: 100px;
    width: 100px;
    background: blue;
    border: 1px black solid;
}
</style>
</head>
<body>
<div id='yea'></div>
<input type="button" value="Capacity Chart" onclick="animateMe();" >
<script>

function animateLeft(obj, from, to){
   if(from >= to){         
       obj.style.visibility = 'hidden';
       return;  
   }
   else {
       var box = obj;
       box.style.marginLeft = from + "px";
       setTimeout(function(){
           animateLeft(obj, from + 1, to);
       }, 25) 
   }
}

function animateMe() {
animateLeft(document.getElementById('yea'), 100, 700);
}
</script>
</body>
</html>

https://jsfiddle.net/geh7ewLx/

于 2013-08-30T21:34:47.107 回答