JQuery动画不起作用

时间:2013-08-29 17:26:03

标签: javascript jquery css

当我点击链接时,我正试图让列表项向上和向下滚动。我无法让它发挥作用。

更新:添加了JSFiddle

jQuery的:

$(document).ready(function() {      
    //console.log($('.nav-up'));
$('.nav-controls').on('click', '.nav-up', function(e) {
    e.preventDefault();
    //alert('clicked');
    var navHeight = $(e.currentTarget).closest('.horz-scroll').find('.block-nav').height();
    var el = $(e.currentTarget).closest('.horz-scroll').find('.block-nav > ul');
    if((el.height() - navHeight) < el.position().top) {
        el.animate({ top: '+=19' }, 'fast');
        console.log(el.css('top'));
    }
});});

HTML:

    <div class="horz-scroll">
<div class="block-nav-wrapper">
<div class="block-nav">
<ul>
    <li>
    <div class="wrap-box-name">&nbsp;<span><img src="../../../upload/1/img/arrow.png" /></span>&nbsp;Orinda Union School District</div>
    </li>
    <li>
    <div class="wrap-box-name">&nbsp;<span><img src="../../../upload/1/img/arrow.png" /></span>&nbsp;Orinda Union School District</div>
    </li>
    <li>
    <div class="wrap-box-name">&nbsp;<span><img src="../../../upload/1/img/arrow.png" /></span>&nbsp;Orinda Union School District</div>
    </li>
    <li>
    <div class="wrap-box-name">&nbsp;<span><img src="../../../upload/1/img/arrow.png" /></span>&nbsp;Orinda Union School District</div>
    </li>
    <li>
    <div class="wrap-box-name">&nbsp;<span><img src="../../../upload/1/img/arrow.png" /></span>&nbsp;Orinda Union School District</div>
    </li>
</ul>
</div>
</div>

<div class="nav-controls">
<ul>
    <li><a class="nav-up" href="#"><img src="../../../upload/1/img/prev.jpg" /></a></li>
    <li><input class="nav_down" name="submit" src="../../../upload/1/img/next.jpg" type="image" /></li>
</ul>
</div>
</div>

CSS:

.nav-controls {
    right: 10px;
    top: 25px;
    margin-left: 37%;
}

.nav-controls ul {
    list-style: none;
    margin: 0;
    padding: 0;
}
.nav-controls ul li{
    display: inline;
    padding-right: 16px;
}
.block-nav-wrapper {
    position: relative;
    margin-bottom: 10px;
    padding: 10px;
}

.block-nav {
    position: relative;
    height: 130px;
    margin: 10px;
    overflow: hidden;
}
.block-nav ul{

}
.block-nav ul li{
    list-style:none;
    line-height:35px;
}


console.log(el.height()); = 175
console.log(navHeight); = 130
console.log(el.position().top); = 0

1 个答案:

答案 0 :(得分:1)

在你得到答案之前要做几件事。

  1. 如果你正在使用它,你必须在你的小提琴中包含jQuery。
  2. Chris Rockwell所述,使用占位符网站(我将所有内容都交换使用http://placehold.it/
  3. 现在,这是小提琴:http://jsfiddle.net/QmxWc/1/。缺少的主要内容是position:relative您试图移动的<ul>。但是你的javascript也存在一些逻辑问题。

    CSS:

    .block-nav ul {
        position: relative;
    }
    

    JS:

    $('.nav-controls').on('click', '.nav-up', function (e) {
        e.preventDefault();
        //alert('clicked');
        var navHeight = $(e.currentTarget).closest('.horz-scroll').find('.block-nav').height();
        var el = $(e.currentTarget).closest('.horz-scroll').find('.block-nav > ul');
        if ((el.height() - navHeight) > Math.abs(el.position().top)) {
            el.animate({
                top: '-=19'
            }, 'fast');
            console.log(el.css('top'));
        }
    });