Javascript JQuery 设置 CSS 转换,转换类中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37082097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery set CSS Transform, Translate Properties in Class
提问by BlunderCode
I cannot seem to set the CSS properties of transform using Jquery.
我似乎无法使用 Jquery 设置转换的 CSS 属性。
Here is my Code: https://jsfiddle.net/op7Lsvdp/
这是我的代码:https: //jsfiddle.net/op7Lsvdp/
These are the two methods I have unsuccessfully tried.
这是我试过失败的两种方法。
$('.slideToCart').css({
'-webkit-transform' : 'translate(left, top)',
'-moz-transform' : 'translate(left, top)',
'-ms-transform' : 'translate(left, top)',
'-o-transform' : 'translate(left, top)',
'transform' : 'translate(left, top)'
});
$('.slideToCart').css('transform', 'translate(50px, 50px)');
$('.slideToCart').css('-webkit-transform', 'translate(50px, 50px)');
Thank You,
谢谢你,
采纳答案by gevorg
I think important thing about cssmethod is that it is not changing your classproperty, but it is changing styleproperty of object that you are calling it on.
我认为关于css方法的重要一点是它不会改变你的类属性,而是改变你正在调用它的对象的样式属性。
This will do:
这将:
$( document ).ready(function() {
$('.buttonHolder').click(function(){
$(this).find("span").toggleClass('fadeText');
var button = $(this).find("button");
button.toggleClass('shrink');
var position = $('.target').position();
var left = position.left + 'px';
var top = position.top + 'px';
var buttonHolder = $(this);
setTimeout(function() {
buttonHolder.css({'transform' : 'translate(' + left +', ' + top + ')'});
}, 1000);
});
});
回答by yezzz
You're adding styles to .slideToCart
elements, but there are none! See the console log:
您正在向.slideToCart
元素添加样式,但没有样式!查看控制台日志:
https://jsfiddle.net/op7Lsvdp/2/
https://jsfiddle.net/op7Lsvdp/2/
You're adding those classes after click on .buttonHolder
, so maybe that's the time you want to add the .css
您在单击后添加这些类.buttonHolder
,所以也许这就是您想要添加.css