利用CSS中input製作開關、輪播圖

樑志芳發表於2019-02-16

1.利用input製作開關

<input type=”checkbox” name=”hobby”>吃飯
<input type=”checkbox” name=”hobby”>睡覺
<input type=”checkbox” name=”hobby”>打豆豆

以上是常用的input核取方塊用法。
我們可以利用核取方塊點選—選中,再點選-不選中的特點製作開關。
開關效果,裡面不僅有效果圖,也有程式碼,左上角的html和css可以點選檢視喔!!!!

2.利用input製作輪播圖

<input type=”radio” name=”gender”>男
<input type=”radio” name=”gender”>女

以上是常用的input單選框用法。
我們可以利用單選框只能選中其中一個製作輪播圖。

輪播圖效果
由於程式碼比較長,就貼上下。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        input{
            display: none;
        }
        ul{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            width: 500px;
            height: 500px;
            margin: 0 auto;
            border:5px solid black;
            /*超出部分隱藏*/
            overflow: hidden;
            position: relative;
            text-align: center;
        }
        .list{
            /*讓ul橫著排放超出box*/
            width: 400%;
            /*利用position:absolute不佔位*/
            position: absolute;
            /*過渡執行時間為1s*/
            transition:1s;
        }
        .list li{
            float:left;
            width: 500px;
            height: 500px;
        }
        label{
            width: 10px;
            height: 10px;
            border-radius: 50%;
            border: 5px solid white;
            position: absolute;
            bottom:40px;
            z-index: 2;
        }
        /*設定每個圓圈的位置*/
        label:nth-of-type(1){
            left:180px;
        }
        label:nth-of-type(2){
            left:220px;
        }
        label:nth-of-type(3){
            left:260px;
        }
        label:nth-of-type(4){
            left:300px;
        }
        input:checked+label{
            background:black;
        }
        /*選中移動ul*/
        input:nth-of-type(1):checked~ul{
            left:0;
        }
        input:nth-of-type(2):checked~ul{
            left:-100%;
        }
        input:nth-of-type(3):checked~ul{
            left:-200%;
        }
        input:nth-of-type(4):checked~ul{
            left:-300%;
        }
    </style>
</head>
<body>
<div id="box">
    <input checked type="radio" name="pic" id="one">
    <label for="one"></label>
    <input type="radio" name="pic" id="two">
    <label for="two"></label>
    <input type="radio" name="pic" id="three">
    <label for="three"></label>
    <input type="radio" name="pic" id="four">
    <label for="four"></label>
    <ul class="list">
        <li style="background: red;"></li>
        <li style="background: blue;"></li>
        <li style="background: yellow;"></li>
        <li style="background: brown"></li>
    </ul>
</div>
</body>
</html>

相關文章