jquery 星星评定系统点击功能

ep6jt1vc  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(79)

我写了一个评级系统的代码星星评级系统基本上,这是所有工作罚款和良好的代码是

<script type="text/javascript">
(function($){

   $(document).ready(function() {
    var $option = $('.option');
    var $fruit = $('.fruit-name');
    var $last;
    var parent
    $option.click(function() {
        parent=$(this).parents('.comment-form-rating');
        $('.fruit-name',parent).val(this.innerHTML)
    })

$(".starr1").hover(function(){
    parent=$(this).parents('.comment-form-rating');
    $('.starr1',parent).css('color','gold')
    },function(){
    $(".starr1").css("color","#ddd");
  });

$(".starr2").hover(function(){
    parent=$(this).parents('.comment-form-rating');
    $('.starr1',parent).css('color','gold')
    $('.starr2',parent).css('color','gold')
    },function(){
    $(".starr1").css("color","#ddd");
    $(".starr2").css("color","#ddd");
  });

$(".starr3").hover(function(){
    parent=$(this).parents('.comment-form-rating');
    $('.starr1',parent).css('color','gold')
    $('.starr2',parent).css('color','gold')
    $('.starr3',parent).css('color','gold')
    },function(){
    $(".starr1").css("color","#ddd");
    $(".starr2").css("color","#ddd");
    $(".starr3").css("color","#ddd");
  });

$(".starr4").hover(function(){
    parent=$(this).parents('.comment-form-rating');
    $('.starr1',parent).css('color','gold')
    $('.starr2',parent).css('color','gold')
    $('.starr3',parent).css('color','gold')
    $('.starr4',parent).css('color','gold')
    },function(){
    $(".starr1").css("color","#ddd");
    $(".starr2").css("color","#ddd");
    $(".starr3").css("color","#ddd");
    $(".starr4").css("color","#ddd");
  });

$(".starr5").hover(function(){
    parent=$(this).parents('.comment-form-rating');
    $('.starr1',parent).css('color','gold')
    $('.starr2',parent).css('color','gold')
    $('.starr3',parent).css('color','gold')
    $('.starr4',parent).css('color','gold')
    $('.starr5',parent).css('color','gold')
    },function(){
    $(".starr1").css("color","#ddd");
    $(".starr2").css("color","#ddd");
    $(".starr3").css("color","#ddd");
    $(".starr4").css("color","#ddd");
    $(".starr5").css("color","#ddd");
  });

});

})(jQuery);
</script>

字符串
但是我必须为我的星星定义一个点击函数,当用户点击星星5时,所有的星星都变成红色--
我是这样定义的

$(".starr5").click(function () {
    parent = $(this).parents('.comment-form-rating');
    $('.starr1', parent).css('color', '#FF7777')
    $('.starr2', parent).css('color', '#FF7777')
    $('.starr3', parent).css('color', '#FF7777')
    $('.starr4', parent).css('color', '#FF7777')
    $('.starr5', parent).css('color', '#FF7777')
});


这里的问题发生了-现在星星成为红色,但在鼠标左边,他们再次成为灰色和鼠标在黄色功能的悬停开始工作- totatly困惑如何编写进一步的代码,使其工作。

tuwxkamq

tuwxkamq1#

您应该只在enter和leave时添加和删除类,所以当
关于mouseenter

.addClass('gold')

字符串
关于click

.removeClass('gold').addClass('red');


关于mouseleave

.removeClass('gold');


你做得很好;快乐编程
根据要求提供Star 1示例:

<style type="text/css">
        .gold{color: gold;}
        .red{color: #FF7777;}
        .yourDefaultStarClass{color: #ddd;}
    </style>

    <script type="text/javascript">
    (function($){
        $(".starr1").hover(function(){
        parent=$(this).parents('.comment-form-rating');
        $('.starr1',parent).addClass('gold');
        },function(){
        $(".starr1").removeClass('gold');

        });

        $(".starr5").click(function () {
        parent = $(this).parents('.comment-form-rating');
        $('.starr1', parent).removeClass('gold').addClass('red');
        }); 

    });          
    </script>

2w3kk1z5

2w3kk1z52#

基于这个Stack Overflow的答案:Turn number into stars rating使用普通CSS创建星星评级系统,这里是添加click功能所需的基本JavaScript

const starRating = (el) => {
  el.addEventListener("click", (evt) => {
    const pointerX = evt.pageX - el.offsetLeft;
    const rating = Math.max(1, Math.min(5, Math.ceil(pointerX / el.offsetWidth * 5)));
    el.style.setProperty("--rating", rating);
    console.log(rating); // @TODO: Send rating value to server
  });
};

document.querySelectorAll("[style^=--rating]").forEach(starRating);
[style^=--rating]::after {
  content: "★★★★★";
  font-size: 4em;
  white-space: nowrap;
  background: linear-gradient(90deg, #fb0 calc(var(--rating) * 20%), #ddd calc(var(--rating) * 20%));
  background-clip: text;
  -webkit-background-clip: text;
  color: #0000;
  cursor: pointer;
}
<span style="--rating:0"></span>

对于在pointermove、pointerdown上改变的UX颜色;记住选择等等。- 使用必要的事件侦听器扩展上面的内容,并使用--color var()扩展CSS,如:

const ratingFromPoint = (evt) => {
  const el = evt.currentTarget;
  const pointerX = evt.pageX - el.offsetLeft;
  return Math.max(1, Math.min(5, Math.ceil(pointerX / el.offsetWidth * 5)));
};

const starRating = (el) => {
  const colorDefault = getComputedStyle(el).getPropertyValue("--color");
  const colorClick = "#f00";
  let ratingSelected = 0;
  
  el.addEventListener("pointerdown", (evt) => {
    ratingSelected = ratingFromPoint(evt);
    el.style.setProperty("--color", colorClick);
    el.style.setProperty("--rating", ratingSelected);
  });
  
  el.addEventListener("pointermove", (evt) => {
    evt.preventDefault();
    const ratingHover = ratingFromPoint(evt);
    el.style.setProperty("--rating", ratingHover);
  });
  
  el.addEventListener("pointerleave", (evt) => {
    el.style.setProperty("--color", colorDefault);
    el.style.setProperty("--rating", ratingSelected);
  });

  el.addEventListener("pointerup", (evt) => {
    ratingSelected = ratingFromPoint(evt);
    console.log(ratingSelected); // @TODO: Send ratingSelected value to server
  });
};

document.querySelectorAll("[style^=--rating]").forEach(starRating);
[style^=--rating] {
  --color: #fb0;
  display: inline-block;
  touch-action: none;
  user-select: none;
  cursor: pointer;
}

[style^=--rating]::after {
  content: "★★★★★";
  font-size: 4em;
  white-space: nowrap;
  background: linear-gradient(90deg, var(--color) calc(var(--rating) * 20%), #ddd calc(var(--rating) * 20%));
  background-clip: text;
  -webkit-background-clip: text;
  color: #0000;
}
<span style="--rating:0"></span>

相关问题