根据URL部分更改背景图像
Change background image based on URL parts
我正在尝试将该功能添加到我的网站中,其中背景将使用样式表根据URL的一部分进行更改。
示例:
address/shop/index.php = background image 1
address/shop/index.php?cPath=22 = background image 2
address/shop/index.php?cPath=23 = background image 3
address/shop/index.php?cPath=24 = background image 4
有什么想法吗?我看过Javascript和jQuery,但不确定该选择哪一个或如何进行
看起来你在使用PHP,所以你应该能够在没有javascript的情况下完成它。您只需要使用PHP的服务器变量。
类似的东西
$cPath = $_GET['cPath']; //This allows access to the query part of the url
if($cPath == 24){
//set background url
}
您可以总是让url的一部分被一个标签包围,例如<font>
所以代码应该是这样的:
<a>your url link
<font class='background1'>part with background1</font>
<font class='background2'>part with background2</font>
</a>
然后在css中可以有
.background1{background: url('...')}
.background2{background: url('...')}
这可能是一种很粗糙的方式,但通过这种方式,你至少可以对你的"链接部分"进行分类。
如果你真的想做这个客户端,我建议:
var pageBackground = {
'default' : 'classOne',
'22' : 'classTwo',
'23' : 'classThree'
};
document.body.className = pageBackground[window.location.split('cPath=')[1] || 'default'];
结合样式表:
body.classOne {
background-image: url(path/to/defaultImage.png);
}
body.classTwo {
background-image: url(path/to/imageTwo.png);
}
body.classThree {
background-image: url(path/to/imageThree.png);
}
在您的html页面上的body标记类型内:
<body background="your file">
</body>
你可以在每个想要不同背景的页面上这样做,它将覆盖任何为主体设置样式的CSS样式
相关文章:
- 可以在这里为背景图像设置滤镜吗
- 使用 js 将背景图像设置为 HTML
- 如何使用css动画/javascript使具有背景图像的元素出现
- 循环浏览多个身体背景图像
- 背景图像顶部的滚动图像不移动
- 在pdfmake中嵌入背景图像
- 修复选择菜单时的背景图像
- 使用css()设置背景图像;不起作用
- 鼠标悬停时如何居中放大背景图像
- 强制谷歌为javascript背景图像编制索引
- 淡入不带HTML的背景图像
- 具有淡入淡出效果的全背景图像
- 未加载随机背景图像
- 背景图像前显示Javascript警报
- javascript背景图像位置循环
- CSS动画背景图像的过渡越来越暗
- "Ken Burns效应”;悬停背景图像
- 通过ID JavaScript显示随机背景图像
- JavaScript背景图像
- 带有图像和背景图像的ClipTo