如何使用变量+=
how to use variable +=
如何在scrollTop()函数'+=' + itemHeight + 'px'
中使用变量http://codepen.io/anon/pen/mVwOwy
var itemHeight = $(item).height(); // item height
//keyup
if (event.which == 38) {
$(box).stop().animate({scrollTop: '-=' + itemHeight + 'px'}, 100);
event.preventDefault();
console.log('-='+ itemHeight + 'px');
// if active item is first child
if (active.index() !== 0) {
$(active).removeClass('active').prev().addClass('active');
console.log('123');
}
}
编辑
你的代码很好,你只是做错了一件小事。你在按下键时也使用了"-=",这是你想要的相反效果。我把它改成了"+=",它把高度加到了当前值上,这就成功了。下面的代码是您需要的:)
按键
$(box).stop().animate({
scrollTop: '-=' + itemHeight
}, 100);
按键
$(box).stop().animate({
scrollTop: '+=' + itemHeight
}, 100);
代码段
$(document).ready(function() {
//item
var box = $('.sel-wrapper');
var item = $('.sel-wrapper li');
var active = $('.sel-wrapper li.active');
var itemHeight = $(item).outerHeight(); // item height
console.log(itemHeight);
$(item).on('click', function() {
$(item).removeClass('active');
$(this).addClass('active');
});
//
var counter = 0;
$(document).keydown(function(event) {
var box = $('.sel-wrapper');
var item = $('.sel-wrapper li');
var active = $('.sel-wrapper li.active');
//keyup
if (event.which == 38) {
$(box).stop().animate({
scrollTop: '-=' + itemHeight
}, 100);
event.preventDefault();
// if active item is first child
if (active.index() !== 0) {
$(active).removeClass('active').prev().addClass('active');
console.log('123');
}
}
//keydown
if (event.which == 40) {
$(box).stop().animate({
scrollTop: '+=' + itemHeight
}, 100);
event.preventDefault();
// if active item is last child
if (active.index() !== item.length - 1) {
$(active).removeClass('active').next().addClass('active');
}
}
counter++;
});
});
* {
box-sizing: border-box;
font-family: sans-serif;
}
body {
margin: 0;
padding: 0;
}
.row {
width: 100%;
}
.sel-wrapper {
padding: 10px 0;
overflow-y: auto;
margin: 20px auto;
width: 200px;
height: 100px;
border: 1px solid black;
}
.sel-wrapper ul {
list-style: none;
padding: 0;
margin: 0;
}
.sel-wrapper li {
padding: 4px 10px;
cursor: pointer;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.sel-wrapper li:hover {
background: rgba(60, 63, 65, 0.49);
}
.sel-wrapper li.active {
background: #1a8bff;
color: #fff;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sel-wrapper">
<ul>
<li class="active">123</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
</div>
相关文章:
- 将require('..')与变量一起使用与在webpack中使用字符串相比
- 在Javascript中添加多个变量以使用RegEx函数
- Ionic:AngularJS变量未使用$scope更新DOM
- 是否可以在另一个变量中使用未声明的变量
- 将jQuery.css()与变量一起使用
- 在URL中发布多个变量并使用复选框
- 在Javascript中的变量内部使用while()语句
- AngularJS字符串变量到使用sanitize的HTML元素
- 为什么我不能在变量上使用内部HTML属性
- 在MVC视图中的变量中使用javascript 从@模型中提取
- 在另一个作用域变量中使用AngularJS作用域变量
- 将where子句与javascript变量一起使用
- 如果变量在使用它的块内被修改,则可以ng
- 从一个函数传递到另一个函数的变量在使用jQuery的.post之后变得未定义
- 可以将ng模式与变量一起使用吗
- 在 PHP 变量中使用 JS
- javascript中正则表达式(match)中变量的使用错误
- 如何在变量中使用本地存储值
- 在Javascript中,我可以在声明变量之前使用它吗
- 为什么要将控制器声明为自身中的变量或使用$scope