简体   繁体   English

如何使用带有css3前缀的jquery数据属性添加内联css

[英]How can I add inline css using jquery data attribute with css3 prefix

This Code is good Working for background size inline css ex: 这段代码很好地用于背景大小内联css ex:

style="background-size:cover"

$(".tp-sliderSection .item").css('background-size', function(){
  $(this).css('background-size',$(this).data("bg-size"));
});

<div data-bg-size="cover"></div>

But I need to use it 但是我需要用它

$(".tp-sliderSection .item").css('background-size', function(){
  $(this).css('background-size',$(this).data("bg-size"));
  $(this).css('-webkit-background-size',$(this).data("bg-size"));
  $(this).css('-moz-background-size',$(this).data("bg-size"));
});

Output css style I need 我需要的输出CSS样式

  style="background-size:cover; -webkit-background-size:cover; -moz-background-size:cover"

ETC 等等

Your original code should have been working fine, but in case it didn't, you can try .attr() . 您的原始代码应该可以正常工作,但是如果不行,则可以尝试.attr() I had often seen that sometimes there's a problem with .data() but .attr() seems to be the fix. 我经常看到有时.data()有问题,但是.attr()似乎可以解决。

Also read: http://api.jquery.com/css/ : 另请参阅: http : //api.jquery.com/css/

As of jQuery 1.8, the .css() setter will automatically take care of prefixing the property name. 从jQuery 1.8开始,.css()设置器将自动处理属性名称的前缀。 For example, take .css( "user-select", "none" ) in Chrome/Safari will set it as -webkit-user-select, Firefox will use -moz-user-select, and IE10 will use -ms-user-select. 例如,在Chrome / Safari中以.css(“ user-select”,“ none”)将其设置为-webkit-user-select,Firefox将使用-moz-user-select,而IE10将使用-ms-user -选择。

Also one thing that is going wrong is returning. 还有一件出错的事情正在回归。 You just need to return the bg-size whereas you are setting it there. 您只需要返回bg-size,即可在此处进行设置。 You had already made the function of background-size and now you just need to return the color. 您已经设置了background-size功能,现在只需要返回颜色即可。 Have a look here: http://jsbin.com/xurevijunu/edit?html,css,js,console,output 在这里看看: http : //jsbin.com/xurevijunu/edit?html,css,js,console,output

As of jQuery 1.4, .css() allows us to pass a function as the property value: 从jQuery 1.4开始,.css()允许我们传递一个函数作为属性值:

$( "div.example" ).css( "width", function( index ) { return index * 50; }); $(“ div.example”).css(“ width”,function(index){返回索引* 50;});

This example sets the widths of the matched elements to incrementally larger values. 本示例将匹配元素的宽度设置为递增的较大值。

Note : If nothing is returned in the setter function (ie. function( index, style ){} ) , or if undefined is returned, the current value is not changed. 注意 :如果在setter function (ie. function( index, style ){} )未返回任何内容function (ie. function( index, style ){} ) ,或者如果返回undefined ,则当前值不会更改。 This is useful for selectively setting values only when certain criteria are met. 仅在满足某些条件时才有选择地设置值很有用。

So, you can try: 因此,您可以尝试:

$(".tp-sliderSection .item").css('background-size', function(){
  return $(this).attr("data-bg-size");
});

Try something like this. 尝试这样的事情。 You can bracket the css with this syntax... 您可以使用以下语法将CSS括起来...

$(".tp-sliderSection .item").css('background-size', function(){
  $(this).css({ 'background-size’ : $(this).data('bg-size’),‘-webkit-background-size' : $(this).data('bg-size’), '-moz-background-size’ : $(this).data('bg-size')  });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM