首先,該練習參考自:https://www.jianshu.com/p/2961d9c317a3 我就直接上代碼了(顏色可以自己調)。 HTML: CSS: green.css red.css blue.css JavaScript: ...
首先,該練習參考自:https://www.jianshu.com/p/2961d9c317a3
我就直接上代碼了(顏色可以自己調)。
HTML:
<nav> <li><a href="#">獨秀不愛秀</a></li> <li><a href="#">獨秀不愛秀</a></li> <li><a href="#">獨秀不愛秀</a></li> <li><a href="#">獨秀不愛秀</a></li> <li><a href="#">獨秀不愛秀</a></li> <li><a href="#">獨秀不愛秀</a></li> </nav> <ul id="skin"> <li id="red"></li> <li id="green" class="current"></li> <li id="blue"></li> </ul>
CSS:
/* 公共部分 */ * { margin: 0; padding: 0; } html, body { font-size: 16px; transition: all 1s; } ul, nav { list-style: none; } /* 網頁皮膚選擇模塊 */ #skin { margin-left: 100px; margin-top: 100px; } #skin li { width: 50px; height: 50px; color: #fff; line-height: 50px; text-align: center; border-radius: 50%; } #skin li + li { margin-top: 10px; } #skin #red { background-color: red; border: 25px solid red; } #skin #green { background-color: #009688bd; border: 25px solid #009688bd; } #skin #blue { background-color: blue; border: 25px solid blue; } #skin li.current { background-color: #fff!important; } #skin li:hover, #skin li:focus { background-color: #fff!important; } /* nav 模塊 */ nav { width: 700px; height: 50px; margin: 100px auto; } nav li { width: 100px; height: 50px; float: left; line-height: 50px; text-align: center; transition: all 1s;
border: 1px solid #fff; } nav li + li { margin-left: 10px; } nav li a { color: #fff; text-decoration: none; } nav li a:hover, nav li a:focus { text-decoration: underline; }
green.css
body { background: #8bc34a; } nav li { background-color: #009688bd; }
red.css
body { background: #9e9e9e5e; } nav li { background-color: #ff5722eb; }
blue.css
body { background: #673ab7a6; } nav li { background-color: #03a9f4a3; }
JavaScript:
window.onload = () => { // 獲取選擇皮膚按鈕 const skinLi = document.getElementById('skin').querySelectorAll('li'); // 獲取 link 標簽 const cssLink = document.getElementById('link'); for (let i = 0; i < skinLi.length; i++) { skinLi[i].addEventListener('click', () => { // 當點擊每一個選擇的時候先去除掉所有選擇按鈕的 class for (ele in skinLi) { skinLi[ele].className = ''; } skinLi[i].className = 'current'; cssLink.href = `css/${skinLi[i].id}.css`; }); } };