可以使用jQuery将旋转添加到当前css值中吗
Can you add rotation to the current css values using jQuery?
例如,我在一页上有3个正方形div,其中一个旋转-20度,一个旋转-10度,另一个旋转10度。当我悬停在它们上面时,我希望它们旋转+10度。
在CSS中,似乎只能设置形状的结束位置,所以如果我把值设为10度,第一个形状将移动30度,而最后一个则根本不会移动。
有没有一种使用jQuery的方法,我可以告诉它从当前位置旋转10度?我不知道是否有任何方法可以将这些css值放入jQuery中,所以我可以添加10,然后作为旋转函数重新输出。
这是所要求的CSS。。。
.photo {
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
transition: all .4s ease-in-out;
position: absolute;
}
.photo:hover {
cursor: pointer;
-webkit-transform:rotate(10deg);
-moz-transform:rotate(10deg);
-o-transform:rotate(10deg);
}
.image-1 {
-webkit-transform:rotate(-20deg);
-moz-transform:rotate(-20deg);
-o-transform:rotate(-20deg);
top: 60px;
left: 60px;
width: 260px;
height: 185px;
}
.image-2 {
-webkit-transform:rotate(-10deg);
-moz-transform:rotate(-10deg);
-o-transform:rotate(-10deg);
top: 60px;
left: 320px;
width: 280px;
height: 210px;
}
.image-3 {
-webkit-transform:rotate(10deg);
-moz-transform:rotate(10deg);
-o-transform:rotate(10deg);
top: 60px;
left: 600px;
width: 280px;
height: 185px;
}
以下是我现在从第一个收到的答案中得到的内容
var angle1 = 10;
var angle2 = 0;
var angle3 = -10;
$('.image-1').css('-webkit-transform','rotate('+angle1+'deg)');
$('.image-2').css('-webkit-transform','rotate('+angle2+'deg)');
$('.image-3').css('-webkit-transform','rotate('+angle3+'deg)');
$('.image-1').mouseover(function(){
angle1b = angle1 + 5;
$(this).css('-webkit-transform','rotate('+angle1b+'deg)');
});
$('.image-1').mouseout(function(){
$(this).css('-webkit-transform','rotate('+angle1+'deg)');
});
$('.image-2').mouseover(function(){
angle2b = angle2 + 5;
$(this).css('-webkit-transform','rotate('+angle2b+'deg)');
});
$('.image-2').mouseout(function(){
$(this).css('-webkit-transform','rotate('+angle2+'deg)');
});
$('.image-3').mouseover(function(){
angle3b = angle3 + 5;
$(this).css('-webkit-transform','rotate('+angle3b+'deg)');
});
$('.image-3').mouseout(function(){
$(this).css('-webkit-transform','rotate('+angle3+'deg)');
});
我意识到这相当笨重,我可能需要一个for?循环为每个图像-1、图像-2等执行相同的功能。我快速尝试了一下,但我对javascript不太熟悉。我的猜测是,这与"图像"+i有关,但我不明白。如果有人能帮忙,我们将不胜感激。然后我所要改变的就是初始变量。
只是一个快速草图:
var angle1 = -20;
var angle2 = -10;
var angle3 = 10;
$('.image-1').click(function(){
angle1 = angle1 + 10;
$(this).css('-webkit-transform','rotate('+angle1+'deg)');
});
等等。
正如您所知道的原始角度,您可以使用纯CSS,使用":hover"来实现这一点。您只需要手动将每个image-n
类的角度设置为比未覆盖的类多10度,例如.image-1
具有transform:rotate(-20deg);
,因此将.image-1:hover
设置为transform:rotate(-10deg);
。
您还可以将过渡添加到未覆盖的版本中,以便在鼠标悬停时获得良好的动画效果。
正如你所建议的,我试图用jQuery做一些更动态的事情,但转换css的存储方式不仅仅是-10deg
,而是作为一个矩阵,所以这将更难做到
以下是更新后的CSS和一个工作片段。我对此所做的唯一其他更改是添加背景色以提高清晰度,并添加非供应商特定的transition
CSS:
.photo {
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
transition: all .4s ease-in-out;
position: absolute;
}
.photo:hover {
cursor: pointer;
-webkit-transform:rotate(10deg);
-moz-transform:rotate(10deg);
-o-transform:rotate(10deg);
}
.image-1 {
-webkit-transform:rotate(-20deg);
-moz-transform:rotate(-20deg);
-o-transform:rotate(-20deg);
transform:rotate(-20deg);
top: 60px;
left: 60px;
width: 260px;
height: 185px;
background-color:red;
}
.image-2 {
-webkit-transform:rotate(-10deg);
-moz-transform:rotate(-10deg);
-o-transform:rotate(-10deg);
transform:rotate(-10deg);
top: 60px;
left: 320px;
width: 280px;
height: 210px;
background-color:green;
}
.image-3 {
-webkit-transform:rotate(10deg);
-moz-transform:rotate(10deg);
-o-transform:rotate(10deg);
transform:rotate(10deg);
top: 60px;
left: 600px;
width: 280px;
height: 185px;
background-color:blue;
}
/* apply a transition to all images for when you come off the hover */
.image-1, .image-2, .image-3 {
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
transition: all .4s ease-in-out;
}
/* for each image set the hover angle to 10 more than the previous */
.image-1:hover {
-webkit-transform:rotate(-10deg);
-moz-transform:rotate(-10deg);
-o-transform:rotate(-10deg);
transform:rotate(-10deg);
}
.image-2:hover {
-webkit-transform:rotate(0);
-moz-transform:rotate(0);
-o-transform:rotate(0);
transform:rotate(0);
}
.image-3:hover {
-webkit-transform:rotate(20deg);
-moz-transform:rotate(20deg);
-o-transform:rotate(20deg);
transform:rotate(20deg);
}
<div id="a1" class="image-1">Image 1</div>
<div id="a2" class="image-2">Image 2</div>
<div id="a3" class="image-3">Image 3</div>
- 在屏幕中央显示图像 CSS jQuery
- 如何打印隐藏文章(Css/JQuery)
- 如何设置DIV背景图像css jquery
- CSS/JQuery/Javascript加载图标仅适用于firefox
- fadeOut”;被禁用”;通过一行看似温和的CSS(jquery bug?)
- CSS: jQuery:将右边距应用于 li 元素,除了每个第 4 个子元素(仅适用于 display:block 的元
- CSS + Jquery Slide在调整大小时切换菜单问题
- 使用 css jquery 设置背景图像
- css JQuery animate 不起作用
- CSS/JQuery 链接悬停不会触发
- 使用 .css jQuery 的 Safari 中的问题
- CSS jQuery - 展示图像(淡入淡出)
- 如何将 Cookie 与点击事件显示的内容一起使用?(HTML/CSS/jQuery)
- 网站加载CSS / Jquery样式缓慢
- 创建一个可调整大小的日历(html/css/jquery)
- CSS/jQuery-响应设计的关闭按钮停留在右上角
- css+jQuery样式.css切换器,用于新的jQuery版本
- Bootstrap 3不允许我在悬停时使用CSS/JQuery图像替换
- (CSS/jQuery)如何用jQuery解决top和bottom属性之间的冲突
- 使用JavaScript更改CSS(jQuery)