CSS转换没有在所有浏览器中使用jQuery进行更新
CSS transform not updating with jQuery in all browsers
我正在尝试翻译html范围输入滑块的input
事件上的一个元素。但是当我检查元素时,jQuery创建的内联样式不会更新,而只是在转换中更新。我应用了一些伪样式,只是为了确保css正在更新,它们确实在更新。其他一切都按预期更新。
如果物品还没有被转换,那么它几乎可以工作一次。但在应用CSS转换后,它不会更新。我在Safari、Chrome和Firefox中进行了测试,结果相同。
方法如下:
useInputValues : function(){
$('input[id$="-x"]').on('input', function(e){
var id = $(this).parents('li').attr('data-item-id'),
item = $('.grid [data-item-id="' + id + '"]'),
x = $(this).val(),
y = $(item).css('transform').split(',')[5] || 0,
trans = 'translate(' + x + 'px, ' + y + 'px)';
console.log(x + ", " + y); // this shows the values are correct and updating properly
// translate the element. Why doesn't this update each time?
$(item).css('transform', trans);
// update the position attribute
$(item).attr('data-x', x); // this happily updates in real time as expected
})
},
进一步的上下文:在没有jQuery的情况下工作
我正在使用interact.js脚本,以便可以拖动项目以获得相同的效果。它使用的函数与我的非常相似,但它可以正常更新(没有jQuery)。
resizeMove : function(event){
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';
x += event.deltaRect.left;
y += event.deltaRect.top;
// Notice here: same general idea, but it's working
target.style.webkitTransform = target.style.transform =
'translate(' + x + 'px,' + y + 'px)';
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
this.updateInputValues(target);
},
非常感谢您的帮助。
编辑:Fiddle
在这里看到它在做同样的事情:https://jsfiddle.net/d5kdmp0v/1/
获取transform
样式的值将返回如下矩阵:
matrix(1, 0, 0, 1, 0, 0)
因为您在逗号上拆分这个字符串,所以结果的第6个元素的字符串中仍然有一个括号,所以字符串是0)
。这是一个无效的CSS值。
如果你在拆分前去掉括号,你就会得到你所期望的:
y = $(item).css('transform').replace(')', '').split(',')[5] || 0
JSFiddle
相关文章:
- jQuery-更新jQuery.ajax().done()中点击的图像
- 基于复选框 Jquery 更新文本框
- 根据复选框选择 jQuery 更新文本框
- JQuery:更新表内容花费的时间太长
- 如何使用Jquery更新数据确认属性中的消息
- Jquery更新到1.10.2后,Jquery Ajax Json对象未显示
- jQuery更新DOM并运行change()方法
- 使用jQuery更新选择的多个字段的数量
- 当原始内容通过jQuery更新时,我如何告诉PageDown更新其预览
- jQuery更新文本字段不起作用
- 有没有更好的方法可以用jquery更新图像的高度和宽度
- 如何使用jquery更新表coulmn
- 使用jQuery更新的Kendo UI网格值不会更新传递回来的模型
- 使用jQuery更新CSS属性
- PrimeFaces 命令按钮使用 jQuery 更新数据表内部
- 创建 Jquery 更新函数
- jquery 更新嵌套对象的键
- 如何使用 jQuery 更新表
- Drupal - Jquery 更新版本与灯箱冲突
- 使用 AJAX Jquery 更新 JSON