首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在鼠标上交换多个图像

问在鼠标上交换多个图像
EN

Stack Overflow用户
提问于 2017-08-07 20:51:59
回答 3查看 787关注 0票数 0

我想交换多个图片上的鼠标,并正在寻找一种方法来做到这一点。我期待着“照亮”多颗星星。下面的代码适用于一颗恒星,但是当光标悬浮在第三颗星上时,什么是照亮3颗恒星的最佳方法呢?

代码语言:javascript
复制
<div class="rating" data-value="4">
 <img src="star.png"/>
 <img src="star.png"/>
 <img src="star.png"/>
 <img src="star.png"/>
</div>

联合来文:

代码语言:javascript
复制
$(".rating > img") .mouseover(function () {
            this.src= "star-on.png";
        }).mouseout(function () {
            this.src= "star.png";

});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-07 21:04:46

您可以这样做,方法是在被滑动的集合中获取恒星的索引,然后根据该索引替换正确的恒星数量:

​

代码语言:javascript
复制
$(".rating > img").mouseover(function () {
    // Get the index of the star that is being hovered over
    var idx = $("img").index(this);
   
    // Loop only over the stars that should be highlighted and highlight them.
    // Use the index to know how many to loop over
    for(var i = 0; i < idx + 1; i++){
      // "Light up" the star:
      $(".rating > img")[i].src= "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Full_Star_Blue.svg/2000px-Full_Star_Blue.svg.png";
    }
  }).mouseout(function () {
        
    // Turn off all stars
    $(".rating > img").each(function(){
    this.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png"
  });

});
代码语言:javascript
复制
img {
 width:30px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rating" data-value="4">
 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png">
 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png">
 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png">
 <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png">
</div>

​

票数 2
EN

Stack Overflow用户

发布于 2017-08-07 21:17:39

我只想提到,这很容易用纯css和柔性盒特性实现:

​

代码语言:javascript
复制
* {box-sizing:border-box;}

body {padding:30px;}

.stars {
  padding:10px;
  background:#fff;
  display:flex;
  flex-direction:row-reverse;
}

.stars .star {
  flex: 0 1 50px;
  background:red;
  padding:20px;
}

.stars .star:hover, .stars .star:hover ~ .star {
  background:salmon;
}
代码语言:javascript
复制
<div class="stars">
  <div class="star">4</div>
  <div class="star">3</div>
  <div class="star">2</div>
  <div class="star">1</div>
</div>

​

票数 0
EN

Stack Overflow用户

发布于 2017-08-07 21:29:25

下面是普通香草JavaScript中的一个示例:

​

代码语言:javascript
复制
var stars = document.querySelector('.stars')

stars.addEventListener('mouseover', function(event) {
  var target = event.target
  var index = target.dataset.index

  for (var i = 0; i < 5; i++) {
    var star = stars.querySelector('[data-index="' + i + '"]')
    star.classList.toggle('active', i <= index)
  }
})

stars.addEventListener('mouseleave', function(event) {
  for (var i = 0; i < 5; i++) {
    var star = stars.querySelector('[data-index="' + i + '"]')
    star.classList.toggle('active', false)
  }
})
代码语言:javascript
复制
.star {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 1px solid black;
  background: white;
}

.star.active {
  background: yellow;
}
代码语言:javascript
复制
<div class="stars">
  <div class="star" data-index="0"></div>
  <div class="star" data-index="1"></div>
  <div class="star" data-index="2"></div>
  <div class="star" data-index="3"></div>
  <div class="star" data-index="4"></div>
</div>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45555526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档