简体   繁体   English

如何使用jquery或javascript单击按钮时向右滑动div?

[英]how to slide div right while on click a button using jquery or javascript?

i want to slide a div when user click on support button from right to left.i have tried the following code.. but its not working correctly for me... 我想在用户从右到左点击支持按钮时滑动div。我已经尝试了以下代码..但它对我不起作用...

<script type="text/javascript">

$(document).ready(function () {
    $('[id$=aSupport]').live('click', function (e) {

        if ($('[id$=hdfsupportcount]').val() == 1) {
            $('[id$=hdfsupportcount]').val(0);

            $('[id$=divSupport]').css({ 'right': '' }).animate({
                'left': '0px'
            });
            // $('[id$=divSupport]').css('display', 'none');
        } else {
            $('[id$=hdfsupportcount]').val(1);

            $('[id$=divSupport]').css({ 'right': '0px', 'left': '' }).animate({
                'right': '30px'
            });
            $('[id$=divSupport]').css('display', 'block');
        }
    });
});
</script>

Html code is: Html代码是:

<div>
    <asp:HiddenField ID="hdfsupportcount" runat="server" />
    <div style="float: right; position: fixed; top: 35%; right: -3px;">
        <a href="javascript:void(0);" class="signin" title="Support" id="aSupport">
            <img src="support_button2.png" /></a>
    </div>
    <div id="divSupport" style="height: 500px; width: 270px; float: right; position: fixed;
        top: 35%; right: 21px; display: none;">
        <div id="ContactMenu">
            <div id="topnav" class="topnav">
            </div>
            <div id="signin_menu">
                <div id="signin">
                    <div style="width: 100%; font-weight: bold; font-size: 22px; color: Black;">
                        <div style="width: 27px; float: left">
                        </div>
                        <div style="float: left; margin-top: 1px">
                            123-456-7890</div>
                    </div>
                    <br clear="all" />
                    <br />
                    <p>
                        <label style="font-weight: bold; color: Black">
                            Contact Us</label>
                    </p>
                    <p>
                        <label>
                            Email</label>
                        <asp:TextBox ID="txtContactEmail" runat="server"></asp:TextBox>
                    </p>
                    <p>
                        <label>
                            Message</label>
                        <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server"></asp:TextBox>
                    </p>
                    <asp:Button ID="btnContactUs" CssClass="signin_submit" runat="server" Text="Send"
                        ValidationGroup="ContactUS" OnClick="btnContactUs_Click" />
                    <asp:Button ID="btnContactUsCancel" CssClass="signin_submit" runat="server" Text="Cancel"
                        OnClick="btnContactUsCancel_Click" />
                </div>
            </div>
        </div>
    </div>
</div>

When we click on buttons first time.the div slides to left but when i click on button again i want to slide it to right and div will be hide. 当我们第一次点击按钮时,div会向左滑动,但是当我再次点击按钮时,我想将它向右滑动,div将被隐藏。 ...please help me... ...请帮我...

I would handle it like this. 我会像这样处理它。

$(function(){
    $(document).on('click', '[id$=aSupport]', function (e) {
        var dist = 0,
            count = $('[id$=hdfsupportcount]'),
            support = $('[id$=divSupport]');
        switch(count.val()){
            case '0':
                    count.val(1);
                    dist = 30;
                break;
            case '1':
                    count.val(0);
                    dist = -300;
                break;
        }    
        support.stop().animate({
            right: dist
        }, 500);
    });
});​

Also, remove display:none; 另外,删除display:none; from <div id="divSupport"> and give it a right:-270px; 来自<div id="divSupport">并给它一个right:-270px; so it appears off the page. 所以它出现在页面外。 It's already hidden from sliding in, so no reason to be display:none; 它已经被隐藏起来了,所以没有理由display:none; . here's a working jsFiddle showing an example . 这是一个有效的jsFiddle,展示了一个例子

I have added the following code and its worked for me.. but still i want if it possible to slide.. like fade in ,fade out.. 我添加了以下代码,它为我工作..但我仍然想要,如果它可以滑动...像淡入淡出,淡出..

My working code is: 我的工作代码是:

<script type="text/javascript">
    $(document).ready(function () {
        $('[id$=aSupport]').live('click', function (e) {

            if ($('[id$=hdfsupportcount]').val() == 1) {
                $('[id$=hdfsupportcount]').val(0);

                $('[id$=divSupport]').css({ 'right': '-27%' }).animate({
                    'right': '-27%'
                });
                // $('[id$=divSupport]').css('display', 'none');
            } else {
                $('[id$=hdfsupportcount]').val(1);

                $('[id$=divSupport]').css({ 'right': '0px', 'left': '' }).animate({
                    'right': '30px'
                });
                $('[id$=divSupport]').css('display', 'block');
            }
        });
    });

</script>

To move the div relative to where it currently is, set the 'marginLeft' property to a value += or -= the amount to move it by. 要将div相对于当前位置移动,请将'marginLeft' property设置为值+ =或 - =将其移动的量。 The above examples effectively work like this: 以上示例有效地工作如下:

$('#example').animate({
    'marginLeft' : "+=50px"
});

and

$('#example').animate({
    'marginLeft' : "-=50px"
});

Here's the full code for the example above which actually calls a function to animate the div. 这是上面示例的完整代码,它实际上调用了一个函数来设置div的动画。

<script language="javascript">
function example_animate(px) {
    $('#example').animate({
        'marginLeft' : px
    });
}
</script>
<input type="button" value="Move Left" onclick="example_animate('-=50px')" />
<input type="button" value="Move Right" onclick="example_animate('+=50px')" />
<div id="example" style="border: 1px solid black; margin: 10px 0px; padding: 10px; background-color: rgb(238, 238, 238); width: 200px; text-align: center;">This is going to move</div>

For more things to do with jquery Click here . 有关jquery的更多信息,请单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM