获取Javascript div以在出现时滑出

获取Javascript div以在出现时滑出,javascript,html,css,Javascript,Html,Css,这里有一点基本的JS,我想滑出,而不是仅仅出现(看起来有问题) 我尝试过一些事情,但就是做不到,有什么想法吗?下面是JSFIDLE后面的代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&

这里有一点基本的JS,我想滑出,而不是仅仅出现(看起来有问题)

我尝试过一些事情,但就是做不到,有什么想法吗?下面是JSFIDLE后面的代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<style>
body{margin:0;}
#foo{min-width:400px; height:100%; background-color:#9C0; display:none; position:absolute; right:0px; top:0px;}


</style>
<body>
<a href="#" onclick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
<div id="foo" >



</div>


<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>
</body>
</html>

无标题文件
正文{页边距:0;}
#foo{最小宽度:400px;高度:100%;背景色:9C0;显示:无;位置:绝对;右侧:0px;顶部:0px;}

如果我正确理解了您的意思,请使用jQuery进行滑动效果

JS:

$("a").on('click', function() {
    $("#foo").slideToggle();
});

我已经为jQuery的使用更新了您的小提琴


我已经用VanillaJS更新了你的小提琴,效果非常好

document.getElementById('bar').onclick = (function()
{
    var that, interval, step = 20,
    id = document.getElementById('foo'),
    handler = function()
    {
        that = that || this;
        that.onclick = null;
        id = document.getElementById('foo');
        interval =setInterval (function()
        {
            id.style.right = (parseInt(id.style.right, 10) + step)+ 'px';
            if (id.style.right === '0px' || id.style.right === '-400px')
            {
                that.onclick = handler;
                clearInterval(interval);
                if (id.style.right === '-400px')
                {
                    id.style.display = 'none';
                }
                step *= -1;
            }
            else
            {
                id.style.display = 'block';
            }
        },100);
    };
    return handler;
}());
守则解释如下:

  • 在JS中附加click处理程序,因为我们必须动态地解除绑定/绑定它
  • 我没有直接分配处理程序,而是使用闭包(用于间隔和DOM引用)
  • handler
    是实际的事件处理程序,它将其上下文(单击的元素)分配给
    ,因此我们可以在间隔回调中引用它并取消绑定处理程序
  • 间隔将元素的位置更改为
    像素(将此值设置为任意值)
  • 如果div位于位置0或-400,则取消间隔(这就是为什么我们需要闭包,以保持间隔ID在范围内,但不全局公开它),并重新绑定单击处理程序,
    step*=-1
    ,以反转动画
  • 设置“显示”属性

这个间隔序列被设置为每100ms发生一次,这有点不稳定,将
set
的值降低到10,并将间隔设置为
50
应该会使事情变得更平滑

为动画引入jquery,而不是解释动画是如何工作的。等等,我通过一个小提琴给出你的解决方案:)来自JS标记描述“如果使用了库或框架,则使用适当的标记来标记问题[…],但是,如果没有使用或没有必要使用框架,则不包括这些标记“.没有JQ标签,没有JQ回答,请对我公平。穆罕默德:我们不需要一直质疑。简单地说,它增加了服务器的负载。但我只是给出了一个小解决方案:'(我的bad@MohammadAreebSiddiqui:提交得太早,很抱歉thatkudos+(+1)对此进行了解释。请检查JS标记说明:“如果使用了库或框架,请使用适当的标记[…]标记问题。”然而,如果框架没有使用或没有必要,不要包含这些标记“不要假设jQ,特别是如果OP没有设置jQ提琴
document.getElementById('bar').onclick = (function()
{
    var that, interval, step = 20,
    id = document.getElementById('foo'),
    handler = function()
    {
        that = that || this;
        that.onclick = null;
        id = document.getElementById('foo');
        interval =setInterval (function()
        {
            id.style.right = (parseInt(id.style.right, 10) + step)+ 'px';
            if (id.style.right === '0px' || id.style.right === '-400px')
            {
                that.onclick = handler;
                clearInterval(interval);
                if (id.style.right === '-400px')
                {
                    id.style.display = 'none';
                }
                step *= -1;
            }
            else
            {
                id.style.display = 'block';
            }
        },100);
    };
    return handler;
}());