使用单击功能而不是切换

时间:2013-07-28 09:45:15

标签: jquery

这是我正在使用的jQuery:

jQuery(document).ready(function () {
    jQuery(".controller a").click(function () {
        jQuery(this).parents('.slider').toggleClass('sliderclick');
        jQuery(this).parents('.slider').animate({left:'0px'},1000);
        jQuery(this).parents('.sliderclick').animate({left:'-200px'},1000);
        return false;
    });
});

Demo

在jsfiddle中,它工作得很好但是当我在我的localhost上尝试它时,它首先在第一次点击时切换,之后它完全可以正常工作。如果我使用toggle()函数它惊人地隐藏说切换a。我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以选择:
检查元素的当前位置并将其转换为布尔值(!parseInt($par.css('left'), 10))。然后,使用一个简单的三元运算符将它分别移到0-200 px:

http://jsfiddle.net/p9jTf/3/

jQuery(function( $ ) {
    $(".controller").click(function ( e ) {
        e.preventDefault();
        var $par = $(this).closest('.slider');        
        $par.animate({left : !parseInt($par.css('left'), 10) ? -200 : 0},1000);
    });
});

这是一个使用类隐藏已打开元素的示例:

http://jsfiddle.net/p9jTf/6/

jQuery(function( $ ) {
    $(".controller").click(function (e) {
        e.preventDefault();
        var $par = $(this).closest('.slider');
        $('.opened').stop().animate({left:-200},1000).removeClass('opened');
        $par.toggleClass('opened').animate({
            left: !parseInt($par.css('left'), 10) ? -200 : 0
        }, 1000);
    });
});

答案 1 :(得分:0)

我在当地试过这个并且它的工作非常好,请检查:

<style type="text/css">
#slider {
    position: fixed;
    top: 0%;
    left:0;
    overflow: hidden;
}
#slider .moduletable {
    margin-bottom: 7px;
}
#slider .moduletable:last-child {
    margin-bottom: 0;
}
.button-wrap > div {
    display: table-cell;
}
.controller {
    width: 55px;
    height: 216px;
    background: #000;
    border-radius: 0 19px 19px 0;
    text-align: center;
    vertical-align: middle;
}
.controller div {
    transform: rotate(90deg);
    white-space: nowrap;
    width: 55px;
    margin-top: -30px;
}
.controller a {
    font-size: 20px;
    line-height: 0;
}
.controller a:hover, .controller a:active, .controller a:visited, .controller a:link {
    text-decoration: none;
}
.content-wrap {
    width: 200px;
    height: 216px;
    background: #403728;
}
.slider {
    position: relative;
    left: -200px;
}
</style>

<!-- Importing jQuery Library -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<!-- Applying Custom JS Functions -->
<script type="text/javascript">
jQuery(document).ready(function ($) {
    $(".controller a").click(function () {
        $(this).parents('.slider').toggleClass('sliderclick');
        $(this).parents('.slider').animate({
            left: '0px'
        }, 1000);
        $(this).parents('.sliderclick').animate({
            left: '-200px'
        }, 1000);
        return false;
    });
});
</script>

<!-- Your HTML -->
<div id="slider">
    <div class="moduletable">
        <div class="slider sliderclick" style="left: -200px;">
            <div class="button-wrap">
                <div class="content-wrap" id="button-1">Booking Contents</div>
                <div class="controller">
                    <div><a href="#buttton-1">Booking</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="moduletable">
        <div class="slider sliderclick" style="left: -200px;">
            <div class="button-wrap">
                <div class="content-wrap" id="button-1">Special Offer Content</div>
                <div class="controller">
                    <div><a href="#buttton-1">Special Offer</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
相关问题