当我单击链接时,我试图让列表项向上和向下滚动。我只是无法让它工作。
更新:添加了一个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"> <span><img src="../../../upload/1/img/arrow.png" /></span> Orinda Union School District</div>
</li>
<li>
<div class="wrap-box-name"> <span><img src="../../../upload/1/img/arrow.png" /></span> Orinda Union School District</div>
</li>
<li>
<div class="wrap-box-name"> <span><img src="../../../upload/1/img/arrow.png" /></span> Orinda Union School District</div>
</li>
<li>
<div class="wrap-box-name"> <span><img src="../../../upload/1/img/arrow.png" /></span> Orinda Union School District</div>
</li>
<li>
<div class="wrap-box-name"> <span><img src="../../../upload/1/img/arrow.png" /></span> 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