单击一个图像然后单击另一个图像时,Jquery悬停图像时出现问题当单击时,单击的第一个图像将不会显示

时间:2015-12-10 07:54:56

标签: javascript jquery html css

现在我遇到一个新问题,当我点击所需的图像评级,然后我点击另一个所需的评级,然后我决定将鼠标悬停在我点击的第一个评级上,现在我第一次点击的星图像评级将不会显示。

例如,假设我点击了3星评级图片,然后点击了1星评级图片,然后我将鼠标悬停在3星评级图片上,图片在悬停时不会显示。

HTML

<form method="" action="" 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() {
  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');
    });
});

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/v6kythze/

2 个答案:

答案 0 :(得分:1)

我今天懒得写javascript,但是你的问题一直在反复播放几个小时,所以这里有一个适合你的版本:

&#13;
&#13;
$(document).ready(function() {
	  var rating;
	  $('.rating-pick li')
	    .on('mouseenter touchstart', function() {
	      var classSuffix = $(this).find('input').attr('id').split('-')[1];
	      $('.rating-pick').addClass('rating-hover-' + classSuffix);
	      $('.rate').text($(this).data('desc'));
	    })
	    .on('mouseleave touchend', function() {
	      var classSuffix = $(this).find('input').attr('id').split('-')[1];
	      $('.rating-pick').removeClass('rating-hover-' + classSuffix);
	      $('.rate').text($('.rate').attr('data-desc'));

	      $('.rating-pick').addClass(rating);
	    })
	    .on('change click', function(e) {
	      $('.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);
	    });
	});
&#13;
* {
  margin: 0;
  padding: 0;
  border: 0;
}
#rating-form ol li {
  list-style: none;
  float: left;
  width: 100%;
}
#rating-form label {
  display: inline-block;
  font-weight: bold;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  cursor: pointer;
}

#rating-form label input{
  display: none;
}

.rate {
  float: left;
  width: 100%;
  margin: -1.4em 0 1.8em 0;
}
.rating-pick {
  width: 150px;
  height: 30px;
  float: left;
  margin-bottom: 1.8em;
  
  background-image: url('http://s8.postimg.org/xgfpw2679/stars.png');
  background-repeat: norepeat;
  background-position: 0px 0px;
}

.rating-1 {
  background-position: 0px -60px;
}
.rating-2 {
  background-position: 0px -120px;
}
.rating-3 {
  background-position: 0px -180px;
}
.rating-4 {
  background-position: 0px -240px;
}
.rating-5 {
  background-position: 0px -300px;
}

.notrated {
  background-position: 0px 0px;
}
.rating-hover-1 {
  background-position: 0px -60px;
}
.rating-hover-2 {
  background-position: 0px -120px;
}
.rating-hover-3 {
  background-position: 0px -180px;
}
.rating-hover-4 {
  background-position: 0px -240px;
}
.rating-hover-5 {
  background-position: 0px -300px;
}

#rating-form .rating-pick li{
  position: relative;
  width: 30px;
  height: 30px;
  display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="" action="" id="rating-form">
  <fieldset>
    <ol>
      <li>
        <ul class="rating-pick">
          <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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是更新的代码:

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

Here is the JSFiddle demo

您的代码中的问题是由于事件在元素上被关闭了。

我删除了该代码并实施了解决方法。