JQuery - 动画

发布于 2024-12-13 15:45:25 字数 716 浏览 1 评论 0原文

我有一个从右侧打开和关闭动画的 div。所以它会滑落并完全消失,然后又滑回去。

但我不想要的是,当它动画关闭时,我不希望它完全消失,我想要 div 偏移 10px。

无论如何要实现这一点吗?

这是 JS:

function hideEditorPane(){
    //weaponEditor
    $("#weaponEditor").animate({
        //left: '+=150'
        width: 'toggle'
        }, 500, function(){
    });
}

这是 CSS:

#weaponEditor{
    position:absolute;
    background-image: url(images/editorPane.png);
    background-repeat: no-repeat;
    width:200px;
    height:342px;
    right:4px;
    top:4px;
}

在 HTML 中,就是这样:

<div id="weaponEditor"><a href="javascript: hideEditorPane();">click</a></div>

谢谢

I have a div that animates on and off from the right. So it slides off and completely disappears, and then slides back on.

What I don't want though, is that when it animates off, I don't want it to completely disappear, I want an offset of the div by 10px.

Anyway of achieving that?

This is the JS:

function hideEditorPane(){
    //weaponEditor
    $("#weaponEditor").animate({
        //left: '+=150'
        width: 'toggle'
        }, 500, function(){
    });
}

Here is the CSS:

#weaponEditor{
    position:absolute;
    background-image: url(images/editorPane.png);
    background-repeat: no-repeat;
    width:200px;
    height:342px;
    right:4px;
    top:4px;
}

And in the HTML, its just this:

<div id="weaponEditor"><a href="javascript: hideEditorPane();">click</a></div>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-12-20 15:45:25

一种可能性:将 overflow:hidden 添加到 #weaponEditor 的 CSS 规则中,然后:

function hideEditorPane(){
    //weaponEditor
    var we = $("#weaponEditor"),
        newWidth = (we.width() < 20) ? 200 : 10;
    we.animate({
            width: newWidth
        }, 500, function(){ });
}

这将在 10 到 200 像素之间切换宽度。

更新:正如 Intersteller_Coder 正确指出的那样,对脚本中的宽度进行硬编码是很糟糕的。如果可能的话,您希望它们保留在 CSS 中。如果切换宽度最终对您有用,请考虑使用 jQuery UI 方法 toggleClass。为折叠宽度设置一个类,为展开宽度设置另一个类,然后使用toggleClass 在两者之间切换。

One posibility: add overflow: hidden to the CSS rules for #weaponEditor, then:

function hideEditorPane(){
    //weaponEditor
    var we = $("#weaponEditor"),
        newWidth = (we.width() < 20) ? 200 : 10;
    we.animate({
            width: newWidth
        }, 500, function(){ });
}

That will toggle the width between 10 and 200 pixels.

Update: As Intersteller_Coder correctly points out, hardcoding the width in the script is terrible. You want those to stay in the CSS, if at all possible. If toggling the width ends up working for you, consider using the jQuery UI method toggleClass. Set up one class for the collapsed width and another class for the expanded width, then use toggleClass to switch between the two.

总攻大人 2024-12-20 15:45:25

虽然如果没有更多地了解你想要实现的目标,很难说清楚,但我想说你想将宽度设置为:10px,而不是使用切换 od。

Toggle 会将宽度设置为 0。

因此使用

function hideEditorPane(){
  //weaponEditor
 $("#weaponEditor").animate({
     //left: '+=150'
     width: "10px"
     }, 500, function(){
 }); }

示例。

也就是说,如果我理解正确的话,您不希望 div 完全消失,您希望该 div 留下 10px,以便稍后可以对其执行某些操作(例如将其折叠并单击展开)。 EM>

Although it's hard to tell without more understanding of what you are trying to achieve I would say that you want to set width to say: 10px instead od using toggle.

Toggle will set width to 0.

So use

function hideEditorPane(){
  //weaponEditor
 $("#weaponEditor").animate({
     //left: '+=150'
     width: "10px"
     }, 500, function(){
 }); }

for example.

That is if I understood correctly that you don't want div to disappear completely you want to have 10px of that div left so you can do something with it later (like folding it to and unfolding at a click).

温折酒 2024-12-20 15:45:25

width:'toggle' 实际上是将 div 从其原始宽度缩小到 0,而不是将其滑动到任何地方。
如果您想继续使用此方法,请在 div 上设置 min-width:10px ,它将阻止 jQuery 将其设置得小于该值。

您已注释掉的代码: left: +=150 是如果想要将其滑出设定量时要使用的代码类型。将 div 向左滑动 500px 会是这样的:

function hideEditorPane(){
    //weaponEditor
    $("#weaponEditor").animate({
        left: '-=500'   //<-- however wide the div is, minus 10px.
        }, 500, function(){
    });
 }

所以显然,将其滑动的量设置为您正在滑动的 div 的宽度,减去您想要“突出”的量

希望这会有所帮助。

width:'toggle' is actually shrinking the div from its's original width to 0, not sliding it anywhere.
If you want to keep using this method, then set a min-width:10px on the div and it will stop the jQuery from making it any smaller than that.

The code you have got commented out: left: +=150 is the type of code to use if want to slide it out by a set amount. It would be something like this to slide the div to the left by 500px:

function hideEditorPane(){
    //weaponEditor
    $("#weaponEditor").animate({
        left: '-=500'   //<-- however wide the div is, minus 10px.
        }, 500, function(){
    });
 }

So obviously, set the amount it slides to the width of the div you are sliding, minus the amount that you want to 'stick out'

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文