将鼠标悬停在图像上时旋转图像

时间:2017-02-15 08:20:22

标签: jquery css twitter-bootstrap-3 hover

我有以下脚本,用户应将鼠标悬停在图像上,然后旋转该图像。



$("#alcazar-image").rotate({
  bind:
  {
    mouseover : function() {
    $(this).rotate({animateTo:180})
  },
  mouseout : function() {
    $(this).rotate({animateTo:0})
    }
  }

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryrotate.com/js/jQueryRotateCompressed.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9 center-block" id="alcazar-row">
          <div class="media">
            <div class="media-left media-middle">
                <a href="#">
                    <img src="http://www.theo-android.co.uk/github-images/alcazar.png" class="media-object img-thumbnail" id="alcazar-image" alt="alcazar">
                </a>
            </div>
            <div class="media-body">
                <div class="media-heading"><h3>Alcazar Park</h3></div>
                <p style="font-size:20px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                <p style="text-align:left"><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#alcazarModal"> More&raquo;</a></p>   
            </div>
              
            </div>  
    </div>
&#13;
&#13;
&#13;

然而,悬停效果并不起作用。有什么想法吗?

谢谢,

西奥。

5 个答案:

答案 0 :(得分:4)

您无需使用JavaScript或jquery来实现您在此处尝试的操作。

一个简单的css transform: rotate应该为你完成这项工作。

&#13;
&#13;
img:hover {
  transform: rotate(720deg);
  -ms-transform: rotate(720deg);
  -webkit-transform: rotate(720deg);
  transition: all 1s;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryrotate.com/js/jQueryRotateCompressed.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9 center-block" id="alcazar-row">
  <div class="media">
    <div class="media-left media-middle">
      <a href="#">
        <img src="http://www.theo-android.co.uk/github-images/alcazar.png" class="media-object img-thumbnail" id="alcazar-image" alt="alcazar">
      </a>
    </div>
    <div class="media-body">
      <div class="media-heading">
        <h3>Alcazar Park</h3>
      </div>
      <p style="font-size:20px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p style="text-align:left"><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#alcazarModal"> More&raquo;</a>
      </p>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

你可以在CSS中完成。

img{
    -webkit-transition: -webkit-transform .8s ease-in-out;
    -ms-transition: -ms-transform .8s ease-in-out;
    transition: transform .8s ease-in-out;


}
img:hover{
    transform:rotate(360deg);
    -ms-transform:rotate(360deg);
    -webkit-transform:rotate(360deg);
}

答案 2 :(得分:1)

我建议将.hover()事件与rotate

一起使用
$("#alcazar-image").hover(function() {
  $(this).toggleClass("rotate-180");
});

JsFiddle demo

答案 3 :(得分:1)

一切都很好,除非您忘记在代码段中包含jquery和jquery-rotate插件,尝试使用以下代码:

&#13;
&#13;
$("#alcazar-image").rotate({
  bind: {
    mouseover: function() {
      $(this).rotate({
        animateTo: 180
      })
    },
    mouseout: function() {
      $(this).rotate({
        animateTo: 0
      })
    }
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://rawgit.com/wilq32/jqueryrotate/master/jQueryRotate.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9 center-block" id="alcazar-row">
  <div class="media">
    <div class="media-left media-middle">
      <a href="#">
        <img src="http://www.theo-android.co.uk/github-images/alcazar.png" class="media-object img-thumbnail" id="alcazar-image" alt="alcazar">
      </a>
    </div>
    <div class="media-body">
      <div class="media-heading">
        <h3>Alcazar Park</h3>
      </div>
      <p style="font-size:20px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p style="text-align:left"><a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#alcazarModal"> More&raquo;</a>
      </p>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:1)

用这个::

替换你的jQuery
$("#alcazar-image").mouseover(function() {
    $(this).css('transform', 'rotate(180deg)');
    $(this).css('-ms-transform', 'rotate(180deg)'); /* IE 9 */
    $(this).css('-webkit-transform', 'rotate(180deg)'); /* Chrome, Safari, Opera */
}).mouseout(function() {
    $(this).css('transform', 'rotate(0deg)');
    $(this).css('-ms-transform', 'rotate(0deg)'); /* IE 9 */
    $(this).css('-webkit-transform', 'rotate(0deg)'); /* Chrome, Safari, Opera */
});

它将完美适用于所有浏览器。