1

我想在使用箭头切换时限制动画的最大高度。尝试编写一个函数,该函数将计算 div 的高度和 +- 以 px 为单位的步长。但它现在被写入变量。

var maxfloorh = $('.floor-switch').height();
var actualh = $('.floor-switch').height();
$('.floor-switch li').click(function() {
    $('#line').animate({
        top: $(this).position().top
    });
});
function floorarrs(numbr) {
    switch (numbr) {
        case 1:
            //check here maxfloorh
            $('#line').animate({
                top: '-=18'
            }, 400, function() {
                maxfloorh-'18';
                console.log(maxfloorh);
            });

        break
        case 2:
              //check here maxfloorh
            $('#line').animate({
                top: '+=18'
            }, 400, function() {
                maxfloorh+'18';
                console.log(maxfloorh);
            });

        break
    }
}
$('.arrw-up').click(function() {
    floorarrs(1);
});
$('.arrw-dwn').click(function() {
    floorarrs(2);
});​

HTML:

<div id="floors" style="margin:60px;">
    <div class="arrw-up"></div>
    <div class="floor-switch">
        <div id='line'></div>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
    <div class="arrw-dwn"></div>
</div>​

直播:http: //jsfiddle.net/cxvec/2/

4

1 回答 1

1
$('.arrw-up').click(function() {
  if ($("#line").css("top") !== "0px") {
    floorarrs(1);
  }
});

$('.arrw-dwn').click(function() {
  if ($("#line").css("top") !== ""+($("#pagination li").length-1)*18+"px") {
    floorarrs(2);
  }
});​

这是工作小提琴

我已经更改了箭头函数中的逻辑,在 css中,元素click的初始属性设置为 ...现在,每当-> div 位于第 1 位,因此该函数不会执行,向下箭头也是如此...注意:这可能是最好的解决方案,但它是解决方案之一topid=line0pxtop = 0pxli

于 2012-11-05T15:35:32.163 回答