【发布时间】:2014-05-17 13:46:17
【问题描述】:
我看不出这段代码有什么问题。计算窗口的中心位置,然后在那里转换-翻译 div(必须使用翻译来完成我正在做的事情)。
刚开始让这适用于 Chrome,因此是 -webkit- 前缀)。
对于为什么 jQuery 不将内联样式应用于 .logo div 非常困惑。已包含我的其他故障排除实验,已注释掉。
语法问题? jsfiddlehere.
var centerPosition = '';
function centerLogo(){
var w_width = $(window).width();
var w_height = $(window).height();
var hCenter = (w_width / 2) - 150;
var vCenter = (w_height / 2) - 150;
console.log(hCenter);
console.log(vCenter);
var centerPosition = 'translate(' + hCenter + 'px, ' + vCenter + 'px);';
console.log(centerPosition);
$('.logo').css('-webkit-transform', centerPosition);
// Try uncommenting the three examples below - they all work ...
// $('.logo').css('background', 'blue');
// centerPosition = 'blue';
// $('.logo').css('background', centerPosition);
// $('.logo').css('-webkit-transform', 'translate(10.85px, 45.56px)');
}
jQuery(document).ready(function($){
centerLogo();
});
【问题讨论】:
-
jQuery 的
css方法,在版本 >= 1.8.0 上,将根据需要添加供应商前缀;无需您手动指定。 -
删除
centerPosition声明中的分号。 jsfiddle.net/Palpatim/7f7rt/5
标签: javascript jquery html css