嗨,我想创建一个滚动到下一个元素的滑块。
我正在使用一个<ul>
和许多<li>
标签,我的所有样式都完全按照我想要的方式设置,但是 JS 在控制台中返回以下错误:
Uncaught TypeError: Cannot read property 'left' of undefined
我拥有的 JS 在下面或查看jsFiddle:
var currentElement = $("li .listofyears:first");
// completely ignoring boundaries
$("#NavigateBackward").click(function() {
currentElement = currentElement.prev();
scrollTo(currentElement);
});
$("#NavigateForward").click(function() {
currentElement = currentElement.next();
scrollTo(currentElement);
});
function scrollTo(element) {
$(window).scrollLeft(element.position().left);
}
我的html如下:
<div id="years_arrows">
<span class="nav_arrows" ><img src="http://iconify.it/wp-content/icons-large-alt/arrow-left.png" id="NavigateBackward" /></span>
<span class="nav_arrows aaa" ><img src="http://upload.wikimedia.org/wikipedia/commons/4/45/Right-facing-Arrow-icon.jpg" id="NavigateForward"/></span>
<div id="slider">
<div id="yearslistwrapper">
<ul class="yearslist">
<li class="listofyears"><a href="#">2003</a></li>
<li class="listofyears"><a href="#">2004</a></li>
<li class="listofyears"><a href="#">2004</a></li>
<li class="listofyears"><a href="#">2005</a></li>
<li class="listofyears"><a href="#">2006</a></li>
<li class="listofyears"><a href="#">2007</a></li>
<li class="listofyears"><a href="#">2008</a></li>
<li class="listofyears"><a href="#">2009</a></li>
<li class="listofyears"><a href="#">2010</a></li>
<li class="listofyears"><a href="#">2011</a></li>
<li class="listofyears"><a href="#">2012</a></li>
<li class="listofyears"><a href="#">2013</a></li>
</ul> <!-- END UL yearslist -->
</div> <!--End slider-->
</div> <!-- End years_arrows div -->