CSS选择所有以前的兄弟姐妹进行星级评定

al狼

我想进行星级评定,但似乎找不到找到悬停时选择所有先前同级的方法。这样的事情是否存在,还是我必须使用JavaScript?

span {
  display:inline-block;
  width: 32px;
  height: 32px;
  background-color:#eee;
}

span:hover {
  background-color:red;
}
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>

更新

我知道只有CSS方式时,我才接受javascript解决方案作为答案。因此,我使用每个人的反馈来发布我的解决方案,任何人都可以将其用作参考并可能提出改进建议。

// Just to check input value
// Consts
const starRadios = document.querySelectorAll('input[name="rating"]');

// EventListeners
starRadios.forEach((radio) => radio.addEventListener('change', getStarRadioValue));

// Get star radio value
function getStarRadioValue(event) { 
    alert(event.target.value) 
    // Do something with it
};
.star-rating {
  font-size: 1.5rem;
  unicode-bidi: bidi-override;
  direction: rtl;
  text-align: left;
}
.star-rating.editable label:hover {
  cursor: pointer;
}
.star-rating.editable .icon-star:hover,
.star-rating.editable .icon-star:hover ~ .icon-star {
  background-color: #fb2727 !important;
}

.icon-star {
  position: relative;
  background-color: #72747d;
  width: 32px;
  height: 32px;
  display: inline-block;
  transition: background-color 0.3s ease;
}
.icon-star.filled {
  background-color: #fb2727;
}

.icon-star > label {
  display: inline-block;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  position: absolute;
}

.icon-star > label > input[type="radio"] {
  position: absolute;
  top: 0;
  left: 0;
  transform: translateY(50%) translateX(50%);
  display: none;
}
<div class="star-rating editable">
  <span class="icon-star">
    <label>
      <input type="radio" name="rating"  value="5" />
    </label>
  </span>
  <span class="icon-star">
    <label>
      <input type="radio" name="rating" value="4" />
    </label>
  </span>
  <span class="icon-star">
    <label>
      <input type="radio" name="rating" value="3" />
    </label>
  </span>
  <span class="icon-star">
    <label>
      <input type="radio" name="rating" value="2" />
    </label>
  </span>
  <span class="icon-star">
    <label>
      <input type="radio" name="rating"  value="1" />
    </label>
  </span>
</div>

韦洛里:

// obtain all spans from DOM
const spans = document.querySelectorAll('span');
// set a variable at global scope as indicator
let flag = false; 

// add event listener to each span
spans.forEach((sp, j)=>{
    sp.addEventListener('click', ()=>{
    // if clicked, then not dismissing the background colour after mouse leave
    flag = true;
    // reassign all spans back to original grey
    spans.forEach(dsp=>{
        dsp.style.backgroundColor = '#eee';
    });
    // assign bg to red of the spans from 0 to clicked index
    Array.from(new Array(j+1), (x, i) => i).forEach(ind=>{
        spans[ind].style.backgroundColor = 'red';
    });
  });
    // redo if mouse enters
    sp.addEventListener('mouseenter', ()=>{
    flag = false;
  });
    // if any span is hovered
    sp.addEventListener('mouseover', ()=>{
    // reassign all spans back to original grey
    spans.forEach(dsp=>{
        dsp.style.backgroundColor = '#eee';
    });
    // assign bg to red of the spans from 0 to clicked index
    Array.from(new Array(j+1), (x, i) => i).forEach(ind=>{
        spans[ind].style.backgroundColor = 'red';
    });
  });
  // in moseleave, only save the background colour if click happened
  sp.addEventListener('mouseleave', ()=>{
    if(!flag){
      spans.forEach(dsp=>{
        dsp.style.backgroundColor = '#eee';
      });
    }
  });
});
span {
  display:inline-block;
  width: 32px;
  height: 32px;
  background-color:#eee;
}

span:hover {
  background-color:red;
  opacity: 0.8;
  cursor: pointer;
}
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CSS选择所有以前的兄弟姐妹

CSS选择所有以前的兄弟姐妹进行星级评定

仅当CSS中有Y个兄弟姐妹时,才如何定位最后N个兄弟姐妹元素?

是否有CSS选择器可用于连续兄弟姐妹

Sitecore Glass Mapper获取所有兄弟姐妹

在Edge和IE上使用Font Awesome进行星级评定无法正常工作

选择兄弟姐妹,但中间不要有兄弟姐妹

隐藏兄弟姐妹选择

之前的兄弟姐妹和以前的元素兄弟姐妹之间的区别

XPATH选择前面所有的兄弟姐妹只选择第一个?我使用的软件有局限性吗?

是否有“反向通用兄弟姐妹” CSS选择器?

如何使用jQuery进行星级评定?

如何使用角度7材质图标按钮进行星级评定?

Clojure拉链可去除所有兄弟姐妹

CSS Hover兄弟姐妹,但没有一个

针对以下所有兄弟姐妹的孩子

通过使用BeautifulSoup选择所有div兄弟姐妹

选择除兄弟姐妹和自我之外的所有元素

选择器不和兄弟姐妹

jQuery选择除自身以外的所有兄弟姐妹和孩子

删除右侧的所有兄弟姐妹,但不删除左侧的所有兄弟姐妹

如何使用jQuery选择元素的所有先前兄弟姐妹?

CSS 影响兄弟姐妹 img

选择 $(this) 元素的父级的兄弟姐妹

如果其中一个具有特定类,则选择所有兄弟姐妹

Xpath - 如何获取兄弟姐妹的所有现有属性名称

相邻兄弟姐妹和一般兄弟姐妹选择器没有正确选择

查询以获取所有兄弟姐妹

CSS 在当前悬停项目之前选择所有兄弟姐妹