js small exercise--replace Baidu page background

Effect realization:

Click on the small image above to change the background of the larger image

 

 html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="css/cute.css" />
		<link rel="stylesheet" type="text/css" href="css/index1.css" />
</head>
<body>
    <div class="skin-container" id="skin-container">
        <img src="" alt="cat1" id="skin-big"/>
    </div>
    <div class="container">
        <ul  class="skin" id="skin">
        <li>
            <img src="img/smalll/1.jpg" alt="cat1">
        </li>
        <li>
            <img src="img/smalll/2.jpg" alt="cat2">
        </li>
        <li>
            <img src="img/smalll/3.jpg" alt="cat3">
        </li>
        <li>
            <img src="img/smalll/4.jpg" alt="cat4">
        </li>
       
    </ul>
    </div>
    <div class="logo"></div>
    <div class="search"></div>
</body>
<!-- 当页面加载完成,载入JavaScript文件,为Html元素添加动作 -->
<script src="js/index1.js" type="text/javascript" charset="utf-8"></script>

</html>

css code:

body{
    height: 100%;
    /* 如果想要保持布局不变,可以给该元素(如div)设置最小宽度属性 */
    min-width: 1000px;
}
div.skin-container {
    width: 100%;
    height: 100%;
    background-color: rgb(245, 179, 179);
    position: fixed;
    top: 0;
    left: 0;
    /* z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。 */
    /* Z-index 仅能在定位元素上奏效(例如 position:absolute;)! */
    z-index: -10;
    
}
div.container{
    width: 100%;
    height: 200px;
    background: #FFF;
    opacity: .87;
}
ul.skin{
    margin: 0px auto;
    padding: 0px;
    list-style: none;
    width: 1040px;
	height: 200px;
}
ul.skin>li{
	width: 260px;
	height: 163px;
    list-style: none;
    float: left;
    cursor: pointer;
    margin-top: 18.5px;
}
ul.skin>li>img{
    border: none;
	outline: none;
    width: 100%;
    height: 100%;
}
div.logo{
    width: 540px;
	height: 258px;
    background: url("../img/logo.png") no-repeat center center;
    margin: 50px auto;
    zoom: 0.5;
}
div.search{
    width: 641px;
	height: 40px;
    background: url("../img/search.png") no-repeat center center;
    margin: 0px auto;
}

js code:

// 获取img列表元素
let skin_img_list=document.getElementById("skin").getElementsByTagName("img");

// 循环遍历img
for(let i=0;i<skin_img_list.length;i++){
    // 添加点击事件
    skin_img_list[i].onclick=function(){
        document.getElementById("skin-container").getElementsByTagName("img")[0].src=this.src;
    }
}

new knowledge:

    /* The z-index property sets the stacking order of elements. Elements with higher stacking order are always in front of elements with lower stacking order. */

    /* Z-index only works on positioned elements (eg position:absolute;)! */

    z-index: -10;

 

 /* If you want to keep the layout unchanged, you can set the minimum width attribute for the element (such as div) */

    min-width: 1000px;

 

Guess you like

Origin blog.csdn.net/qq_63533863/article/details/123658453