Home Web Front-end CSS Tutorial website skin resurfacing

website skin resurfacing

Oct 11, 2016 pm 01:09 PM

网站换肤,之前感觉总是很神奇啊,今天就来总结一下。我写的就是两种思路。

首先都需要建一个css文件夹,里面存放不同颜色的css文件:blue.css; red.css; yellow.css; green.css 在这几个文件中分别写好要改变的样式。

接下来就是html文件,首先第一种思路:只写一个link标签(不推荐,原因请继续阅读)。代码如下:

<!--index.html-->
<head> 
<meta charset="UTF-8">
<title>动态换肤</title>
<link rel="stylesheet" type="text/css" href="css/blue.css">
<style>
*{ margin: 0; padding: 0; }
div{
    height: 50px; 
    background-color: black; 
    padding-left: 10px;
}
div section{
    width: 30px; 
    height: 30px; 
    margin: 10px; 
    display: inline-block; 
}
div section:nth-of-type(1){
    background-color: red; 
}
div section:nth-of-type(2){
    background-color: blue; 
}
div section:nth-of-type(3){
    background-color: green; 
}
div section:nth-child(4){
    background-color: yellow; 
}
</style>
</head>
<body>
<div>
<section data-color="red"></section>
<section data-color="blue"></section>
<section data-color="green"></section>
<section data-color="yellow"></section>
</div>
<script>
var div = document.getElementsByTagName("div")[0];
//添加鼠标单击事件
div.onclick = function(event){
console.log(event.target);
var ele = event.target;
console.log(ele.tagName);//使用.tagName时,控制台输出全部大写,所以在下面的if判断中,使用“SECTION”.
if(ele.tagName == &#39;SECTION&#39;){
var color = ele.dataset.color;
//var color = ele.getAttribute("data-color");
var link = document.createElement("link");
link.href = &#39;css/&#39; + color + ".css";
link.rel = "stylesheet";
// 添加样式表到head,但是会造成页面样式表越来越多,所以不推荐
document.head.appendChild(link); 
}
}
</script>
</body>
Copy after login

第一种思路是只写一个link,然后不断添加样式表到head,但是会造成页面样式表越来越多,所以不推荐。

第二种思路:写4个link标签,然后通过调节link的可用与否来实现网站换肤。代码如下:

<head>
<meta charset="UTF-8">
<title>动态换肤</title>
<link rel="stylesheet" type="text/css" href="css/blue.css">
<link rel="stylesheet" type="text/css" href="css/red.css">
<link rel="stylesheet" type="text/css" href="css/green.css">
<link rel="stylesheet" type="text/css" href="css/yellow.css">
<style>
*{ margin: 0; padding: 0; }
div{ 
    height: 50px; 
    background-color: black;
    padding-left: 10px; 
}
div section{ 
    width: 30px; 
    height: 30px; 
    margin: 10px; 
    display: inline-block; 
}
div section:nth-of-type(1){ 
    background-color: blue; 
}
div section:nth-of-type(2){ 
    background-color: red; 
}
div section:nth-of-type(3){ 
    background-color: green; 
}
div section:nth-child(4){ 
    background-color: yellow; 
}
</style>
</head>
<body>
<div>
<section data-color="0"></section>
<section data-color="1"></section>
<section data-color="2"></section>
<section data-color="3"></section>
</div>
<script>
var links = document.getElementsByTagName(&#39;link&#39;);
function enableLinks(index){
for(var i = 0;i < links.length; i++){//循环查找4个link标签
//disabled表示关闭,如果i不等于当前index,则disabled就是true,即关闭该link标签
//.sheet表示样式表
links[i].sheet.disabled = i!=index;
}
}
enableLinks(2);
//给div标签添加鼠标点击事件
//event:事件对象
document.querySelector(&#39;div&#39;).onclick = function(event){
var index = event.target.dataset.color;
console.log(index);
if(index == undefined){
return;
}else{
//调用enableLinks()
enableLinks(index);
}
}
</script>
</body>
Copy after login

注意两种方法的html部分section的自定义属性data-color,一个是颜色,一个是数字。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

See all articles