我想在我的网站上添加一项功能,允许用户在多种样式之间进行选择。我在其他网站上看到过此功能。我将如何进行此操作,或者有人可以向我推荐有关如何完成此操作的教程或指南?
答案 0 :(得分:5)
首先,您可以简单地使样式表链接动态化:
<link rel="stylesheet" href="<?=$style?>" type="text/css"/>
并提供改变它们的链接:
<a href="/page.php?theme=racecar">Racecar</a>
在服务器上,根据查询字符串分配$style
,如果用户决定修改URL,默认为某些内容也是个好主意:
<?php
$stylesArr = array('racecar', 'magenta', 'cartman');
if(isset($_GET['theme']) && in_array($_GET['theme'], $stylesArr)) {
$style = $_GET['theme'] . '.css';
setcookie("theme", $style, time()+(3600*24*30));//expires in one month
} else {
if(isset($_COOKIE['theme']) && in_array($_COOKIE['theme'], $stylesArr)) {
$style = $_COOKIE['theme'] . '.css';
} else {
$style = 'default.css';
}
}
?>
<link rel="stylesheet" href="<?=$style?>" type="text/css"/>
答案 1 :(得分:0)
如果用户被注册,您可以在Cookie中存储有关样式的信息,也可以在数据库中存储。然后根据值在页面上添加所需的css文件与php。
答案 2 :(得分:0)
根据您的网页呈现方式,最好在选择主题后将主题存储在Cookie中,否则您需要将?theme=racecar
附加到网页上的每个链接。
if(isset($_GET['theme']))
{
setcookie('theme', $_GET['theme']);
$style = $_GET['theme'];
}
else if(isset($_COOKIE['theme']))
{
$style = $_COOKIE['theme'];
}
else
{
$style = 'default';
}
<link href="/styles/<?php echo $style; ?>.css" rel="stylesheet">
答案 3 :(得分:0)
我也看到它用javascript完成。
与其他一些答案相同的基本思路:将所选样式写入cookie,然后在页面加载时读取该样式以确定要加载的样式表。
使用javascript,你可以在不重新加载页面的情况下切换样式,如果你很好地设置你的css,你可以切换身体的id或类,并有一个新的风格,而无需下载一个新的样式表,所以样式切换几乎立即发生。
非常甜蜜的效果,祝你好运。