3

我正在尝试在悬停时将“返回顶部”按钮旋转 360 度,而不会在鼠标离开时不旋转。我已经尝试了多种我发现的 jQuery 代码变体,但我似乎仍然无法让它工作。这是迄今为止我所获得的真实示例(CSS 也可以在图像之间悬停)。

我尝试将 jQuery 更改为mouseenter, mouseoverhover以及;rotate数字后面包含和省略,但无济于事。这是我正在犯的一个简单的 jQuery 语法错误吗?

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;
}

查询:

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

3 回答 3

1

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>

于 2016-09-20T03:50:27.493 回答
1

您可以使用带有旋转动画的 css 变换

.scrollup {


width: 45px;
  height: 45px;
  display: block;
  margin-left: auto;
  position: relative;
  cursor: pointer;

}
.scrollup img {
  position: absolute;
    -webkit-transition: -webkit-transform .8s ease-in-out;
          transition:         transform .8s ease-in-out;

}
.scrollImg2 {
  opacity: 0;
}
.scrollup:hover{

}
.scrollup:hover > img {
  opacity: 0;
  -webkit-transform: rotate(360deg);
          transform: rotate(360deg);
}
.scrollup:hover > .scrollImg2 {
  opacity: 1;
}
于 2016-09-20T03:53:06.373 回答
0

您可以使用css animation, .hover(),animationend事件。事件处理程序animation-name名称的元素@keyframes集,事件空字符串的元素集.hover()animation-nameanimationend

$(".scrollup")
.hover(function() {
  $(this).css({"animationName":"rotate",
               "mozkitAnimationName":"rotate",
               "webkitAnimationName":"rotate"});
})
.on("animationend.rotate", function() {
  $(this).css({"animationName":"",
               "mozkitAnimationName":"",
               "webkitAnimationName":""});
});
.scrollup {
  width: 45px;
  height: 45px;
  display: block;
  margin-left: 50%;
  position: relative;
  cursor: pointer;
  animation-duration: 2s;
  -moz-animation-duration: 2s;
  -webkit-animation-duration: 2s;
}
.scrollup img {
  position: absolute;
}
.scrollImg2 {
  opacity: 0;
}
.scrollup:hover > .scrollImg1 {
  opacity: 0;
}
.scrollup:hover > .scrollImg2 {
  opacity: 1;
}
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
@-moz-keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

于 2016-09-20T05:07:11.537 回答