0

当我单击链接时,我试图让列表项向上和向下滚动。我只是无法让它工作。

更新:添加了一个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
4

1 回答 1

1

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

  1. 如果你使用它,你必须在你的小提琴中包含 jQuery。
  2. 就像Chris Rockwell说的那样,使用占位符网站(我把所有东西都换成了http://placehold.it/

现在,这是小提琴: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'));
    }
});
于 2013-08-29T18:16:08.150 回答