好奇怪啊,明明和老师一模一样,除了图片大小以外。运行的时候动不了,应该是事件绑定那里有问题,求大佬帮忙看一下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>星级评分1.1</title>
<style>
body,li,ul {
padding: 0;
margin: 0;
}
li {
list-style-type: none;
}
.rating {
width: 500px;
height: 460px;
margin: 100px auto;
}
.rating-item {
float: left;
width: 100px;
height: 92px;
background: url('light.jpg')no-repeat;
cursor: pointer;
}
</style>
</head>
<body>
<ul class="rating" id="rating">
<li class="rating-item" title="很差"></li>
<li class="rating-item" title="差"></li>
<li class="rating-item" title="一般"></li>
<li class="rating-item" title="好"></li>
<li class="rating-item" title="很好"></li>
</ul>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
// console.log($);
var num = 2,
$rating = $('#rating'),
$item =$rating.find('.rating-item');
//点亮
var lightOn = function (num) {
$item.each(function (index) {
if (index < num) {
$(this).css('background-position', '0 0px');
} else {
$(this).css('background-position', '0 -100px');
}
});
};
//初始化
lightOn(nun);
//事件绑定
$item.on('mouseover',function(){
lightOn($(this).index()+1);
}).on('click',function(){
num = $(this).index()+1;
});
$rating.on('mouseout',function(){
lightOn(num);
});
</script>
</body>
</html>