jquery - 设置 .css() 多个属性不起作用

标签 jquery css

我试图通过更改 div 背景图像属性来放大图像的一部分。我不确定这是否是正确的方法,但到目前为止它有效!唯一的问题是通过 .css() 传递多个属性。当我单独调用它们而不是一起调用时,这些属性有效!你能告诉我为什么这段代码不起作用吗?

$(document).ready(function () {
    $(".zoom").click(function () {
        $('.gal').css({
            background-size: "200% 200%",
            background-position: "center"
        });
    });
});

创建 Google map 样式图像放大有什么更好的主意吗? 谢谢

最佳答案

改变

$('.gal').css({
  background-size: "200% 200%",
  background-position : "center"
});

DOM 属性符号:

$('.gal').css({
  backgroundSize: "200% 200%",
  backgroundPosition : "center"
});

或在 CSS 属性名称周围添加引号:

$('.gal').css({
  'background-size': "200% 200%",
  'background-position' : "center"
});

参见 documentation :

Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css({'background-color': '#ffe', 'border-left': '5px solid #ccc'}) and .css({backgroundColor: '#ffe', borderLeft: '5px solid #ccc'}). Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name.

关于jquery - 设置 .css() 多个属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17113918/

相关文章:

jquery - 防止固定位置导航栏与粘性页脚重叠

jquery - 如何在magento中触发fancybox html pop的点击

javascript - 如何使用php,mysql在上传之前预览图像

html - 如何止仓: fixed element in the right place in css and html?

html - 带有图像的 Div Css 菜单

HTML & CSS : Positioning and/or Float Issue?

jquery - css 不适用于 jquery mobile 中动态加载的头文件

jquery - Angular - 动态设置 html img 宽度和高度

javascript - 不同的类如何以下划线开头?

CSS :first-child selector IE7