javascript - 单击图像时,Jquery悬停图像出现问题

标签 javascript jquery html css

我遇到的问题是,当我单击所需的图像评级时,当我单击所需的评级后将鼠标悬停在评级较低的图像上时,我无法显示评级较低的图像。

例如,假设我单击了 3 星级图像,我可以悬停并显示 4 星级和 5 星级图像,而无需单击它们,但当我将鼠标悬停在图像上时,无法显示 1 星级和 2 星级图像在我点击 3 星级图像后。

HTML

<form method="post" id="rating-form">
    <fieldset>
        <ol>
            <li>
                <ul class="rating-pick notrated">
                    <li id="rate-1" data-desc="Bad">
                        <label for="rating-1"><input type="radio" value="1" name="rating" id="rating-1" />1 star</label>
                    </li>
                    <li id="rate-2" data-desc="Good">
                        <label for="rating-2"><input type="radio" value="2" name="rating" id="rating-2" />2 stars</label>
                    </li>
                    <li id="rate-3" data-desc="Great">
                        <label for="rating-3"><input type="radio" value="3" name="rating" id="rating-3" />3 stars</label>
                    </li>
                    <li id="rate-4" data-desc="Better">
                        <label for="rating-4"><input type="radio" value="4" name="rating" id="rating-4" />4 stars</label>
                    </li>
                    <li id="rate-5" data-desc="Best">
                        <label for="rating-5"><input  type="radio" value="5" name="rating" id="rating-5" />5 stars</label>
                    </li>
                </ul>
                <div class="rate" data-desc="Rate this product">Rate this product</div>
            </li>
        </ol>
    </fieldset>
</form>

Jquery

$(document).ready(function() {
  $('.rating-pick li')
    .on('mouseenter touchstart', function(){
      var classSuffix = $(this).find('input').attr('id').split('-')[1];
      $('.rating-pick').prevAll().addBack().addClass('rating-' + classSuffix);
      $('.rating-pick').nextAll().removeClass('notrated');
      $('.rate').text($(this).data('desc'));
    })
    .on('mouseleave touchend', function(){        
      var classSuffix = $(this).find('input').attr('id').split('-')[1];
      $('.rating-pick').prevAll().addBack().removeClass('rating-' + classSuffix);
      $('.rate').text($('.rate').attr('data-desc'));
    })
    .on('change click', function(e){
      e.preventDefault();
      e.stopPropagation(); 
      $('.rate').attr('data-desc', $(this).attr('data-desc'));
      var classSuffix = $(this).find('input').attr('id').split('-')[1];
      $('ul.rating-pick').removeClass().addClass('rating-pick rating-' + classSuffix);
      $(this).off('mouseenter touchstart mouseleave touchend');
    });
}); 

CSS

*{
  margin: 0;
  padding: 0;
  border: 0;
}

#rating-form ol li{
  list-style: none;
  width: 100%;
  float: left;
}

#rating-form label{
    display: inline-block;
    margin-bottom: 0.2em;
    font-weight: bold;
    width: 100%;
}

.rate{
    float: left;
    width: 100%;
    margin: -1.4em 0 1.8em 0;
}

.rating-pick{
    width: 150px;
    height: 30px;
    float: left;
    margin-bottom: 1.8em;
}

.notrated{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px 0px;
}


.rating-1{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px -60px;
}

.rating-2{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px -120px;
}

.rating-3{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px -180px;
}

.rating-4{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px -240px;
}

.rating-5{
    background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
    background-repeat: repeat-x;
    background-position: 0px -300px;
}

.rating-pick input[type="radio"], .rating-pick label{
    height: 0 !important;
    display: none !important;
}

.rating-pick li{
    float: left !important;
    width: 30px !important;
    height: 30px !important; 
    display: block !important;
    list-style-type: none !important;
    cursor: pointer !important;
}

这是正在运行的代码的链接 http://jsfiddle.net/pt20141d/1/

最佳答案

我添加了一个变量,这是您生成的代码:

$(document).ready(function() {
  var rating;
      $('.rating-pick li')
        .on('mouseenter touchstart', function() {
          var classSuffix = $(this).find('input').attr('id').split('-')[1];
          $('.rating-pick').prevAll().addBack().addClass('rating-' + classSuffix);
          $('.rating-pick').nextAll().removeClass('notrated');
          $('.rate').text($(this).data('desc'));


        rating = $('.rating-pick').attr("class").split(" ")[1];
        $('.rating-pick').removeClass(rating);
        })
        .on('mouseleave touchend', function() {
          var classSuffix = $(this).find('input').attr('id').split('-')[1];
          $('.rating-pick').prevAll().addBack().removeClass('rating-' + classSuffix);
          $('.rate').text($('.rate').attr('data-desc'));

        $('.rating-pick').addClass(rating);
        })
        .on('change click', function(e) {
          e.preventDefault();
          e.stopPropagation();
          $('.rate').attr('data-desc', $(this).attr('data-desc'));
          var classSuffix = $(this).find('input').attr('id').split('-')[1];
          $('ul.rating-pick').removeClass().addClass('rating-pick rating-' + classSuffix);
          $(this).off('mouseenter touchstart mouseleave touchend');
        });
    });

Here is the JSFiddle demo

(和 this one is for fun :D )

关于javascript - 单击图像时,Jquery悬停图像出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195073/

相关文章:

javascript - 如何在Parse上保存具有不同ACL的Parse对象的指针?

javascript - 将 SweetAlert2 与 TypeScript 一起使用,找不到模块 'sweetalert2/dist/sweetalert2.js' 的声明文件

jquery如何将json数据发送到多个链接?

javascript - 为什么 safari 浏览器显示的字体比 Chrome 等其他浏览器大

javascript - 使用 Javascript 移动 "disabled"类

javascript - 未捕获( promise ): TypeError: Cannot read property 'create' of undefined (ionic 3. 9,Angularjs 5.0.3)

javascript - 使用 JavaScript 隐藏/显示按钮 Nav onClick

html - 如何对像素使用 bootstrap 4 边距和填充属性?

javascript - Materialise CSS sidenav 'options' 未定义

html - IE Chrome 和 Firefox 的定位差异