javascript小练手

1、做一个小图标在界面中通过按钮可以变大 变小 变色 变圆

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>改变图片</title>
    <style>
        .box{//设置显示的盒子的属性
            width:100px;
            height:100px;
            background-color: red;
        }
        button{//设置你的按钮的属性
            width:20px;
            height:20px;
            font-size: 5px;
            /*background-color: green;*/
        }
    </style>
</head>
<body>
<button type="button" id="big">变大</button>
<button type="button" id="small">变小</button>
<button type="button" id="changeColor">变色</button>
<button type="button" id="circle">变圆</button>

<div class="box" id="hezi"></div>
<script type="text/javascript">
    window.onload = function(){
        function $(id){  //把获取id封装成一个函数
            return document.getElementById(id);
        }
        $('big').onclick = function(){//id为big的那个按钮对盒子进行操作
            $('hezi').style.width = '300px';
            $('hezi').style.height = '400px';
        }
        $('small').onclick = function(){
            $('hezi').style.width = '50px';
            $('hezi').style.height = '50px';
        }
        $('changeColor').onclick = function(){
            // $('hezi').style.color = 'yellow';  //错误的写法 你上面定义的是background这也应该是background
            $('hezi').style.backgroundColor = 'yellow';
        }
        $('circle').onclick = function(){
            $('hezi').style.borderRadius = '50%'; //变圆就是设置成50%
        }
    }
</script>
</body>
</html>
改变一个界面中的盒子大小颜色

2、当你的鼠标移动到一个图片上的时候会替换成另外一个图片 当移走的时候又会变成另外一个

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>替换界面图片</title>
</head>
<body>
    <div class="box">
        <img src="./2_01.png" alt="照相机" id="ca" title="哈哈哈">

    </div>
    <script>
        window.onload = function(){
            var oImg = document.getElementById('ca');
            oImg.onmouseover = function(){
                oImg.src = './2_02.png';
                var myDate = new Date();
                // console.log(myDate.toLocaleString());
                // oImg.title = myDate.toLocaleString();
                console.log(myDate.toLocaleString());
                oImg.title = myDate.toLocaleString();
            }
            oImg.onmouseout = function(){
                oImg.src = './2_01.png';
            }

        }
    </script>

</body>
</html>
变换图片

猜你喜欢

转载自www.cnblogs.com/askzyl/p/9111874.html