2012-11-28 42 views
-1

我想要设置:背景图像,背景大小和背景重复...这里的语法如何?在预加载器上使用jQuery .css()设置CSS背景图像

qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({ 
        height: "40px", 
        width: "100px", 
        position: "absolute", 
        background: 'url(images/preload.gif)', 
        fontSize: "3em", 
        top: "50%", 
        left: "50%", 
        color: "#FFFFFF", 
        marginTop: "-" + (59 + qLoptions.barHeight) + "px", 
        textAlign: "center", 
        marginLeft: "-50px", 
       }).appendTo(qLoverlay); 
+0

你有没有想过自己做?这个问题实际上是自己回答的。 –

+0

在CSS中你可以背景:url(images/preload.gif)no-repeat ...',' – Valky

回答

0

为什么当你的代码中有静态属性时,你想混乱你的JavaScript。

最好让他们在单独的并将其添加到您的元素。

这将是更清洁。

这样你也可以指定background image, background size, and background repeat

CSS

.overlay 
{ 
    height: "40px"; 
    width: "100px"; 
    position: "absolute"; 
    background: 'url(images/preload.gif)' 50% 50% repeat-x ; 
    background-size: 275px 125px; 
    fontSize: "3em"; 
    top: "50%"; 
    left: "50%"; 
    color: "#FFFFFF"; 
    textAlign: "center"; 
    marginLeft: "-50px"; 
}​ 

的Javascript

qLpercentage = 
    $("<div id='qLpercentage'></div>").text("0%") 
    .css({ 
     marginTop: "-" + (59 + qLoptions.barHeight) + "px" 
    }) 
    .addClass('overlay') 
    .appendTo(qLoverlay);​ 
0

尝试添加此:

"background-image" : "url(my-image.png)", 
    "background-repeat" : "...", 
    "background-size" : "...", 
0

我有我是正确的它是这样的:

qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({ 
        height: "40px", 
        width: "100px", 
        position: "absolute", 
        backgroundImage: 'url(images/preload.gif)', 
        backgroundRepeat: 'repeat-x', 
        backgroundSize: '10px 10px', 
        fontSize: "3em", 
        top: "50%", 
        left: "50%", 
        color: "#FFFFFF", 
        marginTop: "-" + (59 + qLoptions.barHeight) + "px", 
        textAlign: "center", 
        marginLeft: "-50px", 
       }).appendTo(qLoverlay);