滑鼠懸浮評分效果程式碼例項

admin發表於2017-04-15

分享一段程式碼例項,它實現了滑鼠懸浮評分效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
ul, li {
  margin: 0;
  padding: 0;
  list-style: none;
}
.box {
  width: 300px;
  height: auto;
  margin: 0 auto;
}
ul {overflow: hidden;}
.star > li {
  float: left;
  margin-top: 15px;
  font-size: 32px;
  cursor: pointer;
}
.active {
  color: #f00;
}
.text {
  width: 100px;
  height: 38px;
  line-height: 38px;
  border: solid 1px #ccc;
  text-align: center;
  margin-top: 20px;
  display: none;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
window.onload = function() {
  score();
}
 
function score() {
  var start = document.getElementById("star");
  var startLi = start.getElementsByTagName("li");
  var score = document.getElementById("result");
  var text = document.getElementById("text-word");
  var word = ['很差', '差', '一般', "好", "很好"]
  for (var i = 0, len = startLi.length; i < len; i++) {
    startLi[i].index = i; //給li新增一個索引值
    startLi[i].onmouseover = function() {
      text.style.display = "block";
      //this指當前滑鼠經過的物件li
      text.innerHTML = word[this.index];
      //滑鼠經過當前li,<=當前li的索引值的所有li都變亮
      for (var j = 0; j <= this.index; j++) { 
        startLi[j].className = "active";
      }
    }
 
    startLi[i].onmouseout = function() {
      text.style.display = "none";
      for (var j = 0; j < startLi.length; j++) {
        startLi[j].className = "";
      }
    }
 
    startLi[i].onclick = function() {
      score.innerHTML = (this.index + 1) + "分";
    }
 
  }
}  
</script>
</head>
<body>
  <div class="box">
    <div class="score">
      <span>打分結果</span>
      <span class="result" id="result"></span>
    </div>
    <ul class="star" id="star">
      <li>★</li>
      <li>★</li>
      <li>★</li>
      <li>★</li>
      <li>★</li>
    </ul>
    <div class="text" id="text-word">一般</div>
  </div>
</body>
</html>

相關文章