zoukankan      html  css  js  c++  java
  • 事件代理 方式百度换皮肤

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
    *{margin:0;padding:0}
    body {

    background: url(images/1.jpg) top center;
    }
    .box {
    height: 150px;
    background: rgba(255,255,255,.3);
    text-align: center;
    padding-top:50px;
    }
    .box img {
    cursor: pointer;
    }
    </style>
    <script>
    window.onload = function(){
    /*通过事件代理来解决问题*/
    var box=document.querySelector(".box");
    box.addEventListener("click",function(e){

    if(e.target.tagName.toLowerCase()=="img"){
    //获得图片src 需要解析
    //解析之后是file:///E:/JS学习/04. 京东商城项目实战(共60多集)/2.京东商城-JS/第1天/笔记源码/案例/08 百度换肤/images/1.jpg

    var imgUrl=decodeURIComponent(e.target.src);
    //图片放在哪个文件夹就需要找到文件夹字符串的位置 来找到图片的相对位置
    var sign=imgUrl.lastIndexOf("images");
    //截取需要的路径
    var url=imgUrl.slice(sign);
    document.body.style.backgroundImage='url('+url+')';
    }

    },false);
    }
    </script>
    </head>
    <body>
    <div class="box">
    <img src="images/1.jpg" alt="" width="150" id="pic1"/>
    <img src="images/2.jpg" alt="" width="150" id="pic2"/>
    <img src="images/3.jpg" alt="" width="150" id="pic3"/>
    </div>
    </body>
    </html>

  • 相关阅读:
    react axios 跨域访问一个或多个域名
    基础复习1
    Webpack笔记
    小程序笔记下
    小程序笔记上
    webpack-dev-server的使用
    webpack资源加载常用配置
    vue实现组件切换的两种方式
    vue实现一个评论列表
    子组件调用父组件的数据及方法
  • 原文地址:https://www.cnblogs.com/liveoutfun/p/9174908.html
Copyright © 2011-2022 走看看