jquery背景变化
考虑以下情况。我有 2 种类型的背景: 1.Photoshop 渐变,我使用 CSS 标记,例如:
html{
background-image: url('imgs/gradient.png');
background-repeat: repeat-x;
background-color: #000000;
}
2.背景图案,标记如下
html{
background-image:url('paper.gif');
}
以下 Jquery 负责背景更改:
$(function() {
$('a').click(function() {
var currentLink = $(this);
var which = $(this).attr('id');
//Color scheme variables(image,color)
var image = '';
var color = '';
//gradient + color background
if (which == 'a1')
{image = "url(backgrounds/pat2.png)";
color = "#3a2c33";}
//pattern type background
else if(which == 'a2')
{image = "url(backgrounds/pat4.jpg)";
color = " ";}
if(color != " "){
$('html').css("background-image", image);
$('html').css("background-color", color);
}
else{
$('html').css("background", image);
}
return false;
});
});
如果我选择图案类型背景,然后选择渐变/color 类型背景 背景颜色无法初始化。
Consider the following situation. I've 2 types of backgrounds: 1.Photoshop gradients for which I use CSS markup like:
html{
background-image: url('imgs/gradient.png');
background-repeat: repeat-x;
background-color: #000000;
}
2.Background patterns for which the markup is as follows
html{
background-image:url('paper.gif');
}
The following Jquery is responsible for the background change:
$(function() {
$('a').click(function() {
var currentLink = $(this);
var which = $(this).attr('id');
//Color scheme variables(image,color)
var image = '';
var color = '';
//gradient + color background
if (which == 'a1')
{image = "url(backgrounds/pat2.png)";
color = "#3a2c33";}
//pattern type background
else if(which == 'a2')
{image = "url(backgrounds/pat4.jpg)";
color = " ";}
if(color != " "){
$('html').css("background-image", image);
$('html').css("background-color", color);
}
else{
$('html').css("background", image);
}
return false;
});
});
If I select pattern type background and then gradient/color type background the background color couldn't be initialised.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在
html
上使用一个类并切换它,而不是在 JS 中处理background
CSS 值。Rather than juggle
background
CSS values in JS, you should use a class onhtml
and toggle that.也许您需要清除背景属性,因为它可能会同时保留背景、背景图像和背景颜色?
Maybe you need to clear the background attribute since it may be leaving both background, background-image, and background-color all at once?
我认为背景样式应该在
body
上,而不是在html
上I think that the background style should be on
body
and not on thehtml