【问题标题】:jquery .css() not applying -webkit-transformjquery .css() 不应用-webkit-transform
【发布时间】: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


【解决方案1】:

$('.logo').css('-webkit-transform', centerPosition); 的正确语法在字符串中没有分号。尝试改变这个:

var centerPosition = 'translate(' + hCenter + 'px, ' + vCenter + 'px);';

到这里:

var centerPosition = 'translate(' + hCenter + 'px, ' + vCenter + 'px)';

应该工作:http://jsfiddle.net/7f7rt/6/

【讨论】:

  • 这有什么帮助?
  • $('.logo').css('-webkit-transform', centerPosition); 的正确语法在字符串中没有分号。
  • 你确定吗,因为它在 jsfiddle 中不起作用...请再次检查。
  • Alpesh 下面给出的答案是有效的。分号没有问题。
  • 我怎么会错过呢?谢谢大家的帮助。也很高兴了解 jQuery 前缀。
【解决方案2】:

这肯定会奏效。

    var posElem = document.getElementById('logo'); // set id to div 

    var newStyle = '-webkit-transform: translate(' + hCenter + 'px, ' + vCenter + 'px);' +
   'transform: translate(' + hCenter + 'px, ' + vCenter + 'px);';
   posElem.setAttribute('style',newStyle);

【讨论】:

  • 评论此行。 $('.logo').css('-webkit-transform', centerPosition);并且不要忘记更改您的 div 的 id 徽标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 2020-09-13
  • 2014-01-04
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
相关资源
最近更新 更多