如何使用jquery在悬停时旋转图像? [英] How do I rotate an image on hover using jquery?

查看:88
本文介绍了如何使用jquery在悬停时旋转图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在悬停时将回到顶部按钮旋转360度而不在鼠标上旋转。我已经尝试了多种jQuery代码,我发现但我仍然无法让它工作。这是我到目前为止的真实例子(CSS也在图像之间悬停)。

I am trying to rotate a 'back to top' button 360 degrees on hover WITHOUT un-rotating on mouseleave. I have tried multiple variations of jQuery code that I've found but I still can't seem to get it working. Here's the real example of where I've gotten so far (CSS hover between images as well).

我尝试将jQuery更改为 mouseenter mouseover 悬停以及在 rotate ; $ c>数字,无济于事。我正在制作一个简单的jQuery语法错误吗?

I have tried changing the jQuery to mouseenter, mouseover, hover as well as including and omitting the ; after the rotate number, to no avail. Is it a simple jQuery syntax mistake that I'm making?

HTML:

<div class="scrollup">
  <img src="https://static1.squarespace.com/static/56b92ff8e707ebc576b99166/t/57e099d215d5dbdafb6373aa/1474337234028/top-circleonly.png" class="scrollImg1 scrollup-circle"/>
  <img src="https://static1.squarespace.com/static/56b92ff8e707ebc576b99166/t/57e09a11f5e2318fad09f16f/1474337297146/top-hover-circleonly.png" class="scrollImg2 scrollup-circle"/>
  <img src="https://static1.squarespace.com/static/56b92ff8e707ebc576b99166/t/57e099f3f5e2318fad09f010/1474337267982/top-textarrowonly.png" class="scrollImg1 scrollup-textarrow"/>
  <img src="https://static1.squarespace.com/static/56b92ff8e707ebc576b99166/t/57e09a17f5e2318fad09f1a5/1474337303397/top-hover-textarrowonly.png" class="scrollImg2 scrollup-textarrow"/>
</div>

CSS:

.scrollup {
  width: 45px;
  height: 45px;
  display: block;
  margin-left: auto;
  position: relative;
  cursor: pointer;
}
.scrollup img {
  position: absolute;
}
.scrollImg2 {
  opacity: 0;
}
.scrollup:hover > .scrollImg1 {
  opacity: 0;
}
.scrollup:hover > .scrollImg2 {
  opacity: 1;
}

JQuery:

$(".scrollup").mouseover(function() {
    $(".scrollup-circle").rotate(360);
});


推荐答案

你可以用来做jQuery 如下所示

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	
</head>

<style type="text/css">

div.main{
	width: 100px;
	height: 100px;
	
}

div.main img{
	width: 100%;
	height: 100%;
}

.change{
	-ms-transform: rotate(360deg); /* IE 9 */
    -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
    transform: rotate(360deg);
    transition-duration: 5s;
}

   

</style>


<body>

<div class="main">
<img src="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg">
</div>

<p id="dis"></p>

<script type="text/javascript">


$("div.main").mouseenter(function(){
	$(this).addClass("change").delay(5000).queue(function(){
		$(this).removeClass("change").dequeue();
	});
	
});



</script>




</body>


</html>

注意:(之后)--->我在你上次的评论中没有得到你真正的要求。但请试试这个问题:)。希望它会对你有帮助。

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	
</head>

<style type="text/css">

div.main{
	width: 100px;
	height: 100px;
	
}

div.main img{
	width: 100%;
	height: 100%;
}

.change{
	-ms-transform: rotate(360deg); /* IE 9 */
    -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
    transform: rotate(360deg);
    transition-duration: 5s;
}

.myopacity{
	opacity: 0.6;
}



</style>


<body>

<div class="main">
<img src="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg">
</div>

<p id="dis"></p>

<script type="text/javascript">

var thevalue = 1;
$("div.main").mouseenter(function(){

	thevalue = thevalue+1;
	if(thevalue%2==0)
	{
		$(this).addClass("myopacity");
	}
	else
	{
		$(this).removeClass("myopacity");
	}

	$(this).addClass("change").delay(5000).queue(function(){
		$(this).removeClass("change").dequeue();
	});
	
});



</script>




</body>


</html>

这篇关于如何使用jquery在悬停时旋转图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆