美文网首页
2019-02-13JS应用

2019-02-13JS应用

作者: 遥远的她197 | 来源:发表于2019-02-13 21:00 被阅读0次

    01动态添加和删除案例

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <!--导入工具模块-->
            <script type="text/javascript" src="js/tool.js"></script>
            <style type="text/css">
                .fruitBox{
                    width: 260px;
                    height: 60px;
                    background-color: darkcyan; 
                    margin-left: 2px;
                    
                    line-height: 60px;
                }
                .fruitname{
                    float: left;
                    width: 230px;
                    text-align: center;
                    
                    font-size: 20px;
                    color: white;
                }
                .close{
                    font-size: 20px;
                    color: white;
                    /*设置光标样式*/
                    cursor: pointer;
                }
                #newFruit{
                    border: 0;
                    border-bottom: 1px solid #444444;
                    font-size: 30px;
                    width: 250px;
                    text-align: center;
                }
                /*去光标*/
                #newFruit:focus{
                    outline: none;
                }
                /*确定按钮*/
                #sure{
                    border: 0;
                    background-color: coral;
                    color: white;
                    font-size: 20px;
                    width: 70px;
                    height: 40px;
                    /*设置水平对齐*/
                    vertical-align: bottom;
                }
                #sure:focus{
                    outline: none;
                }
            </style>
        </head>
        <body>
            <!--水果(展示页面)-->
            <div id="show"> 
            </div>
            
            <!--添加-->
            <div id="add">
                <input type="" name="newFruit" id="newFruit" value="" />
                <button id="sure" onclick="addNewFruit()">确定</button>
            </div>
            <!--默认显示的水果-->
            <script type="text/javascript">
                //函数封装
                function creatFruitNode(name){
                    //创建div(水果父节点)
                    var divNode = document.createElement('div')
                    divNode.className = 'fruitBox' //相当于设置class值
                    //文字(水果名字节点)
                    var nameNode = document.createElement('font')
                    nameNode.innerText = name
                    nameNode.className = 'fruitname'
                    // 叉叉(关闭按钮)
                    var closeNode = document.createElement('font')
                    closeNode.innerText = '×'
                    closeNode.className = 'close'
                    //关闭按钮添加点击事件
                    closeNode.addEventListener('click', function(){
                        //叉掉 移除被点击的节点的父节点
                        this.parentElement.remove()
                    })
                    
                    divNode.appendChild(nameNode)
                    divNode.appendChild(closeNode)
                    
                    return divNode
                }
                fruitArray = ['苹果','香蕉','梨','猕猴桃']
                //展示所有水果的节点
                showNode = document.getElementById('show')
                for(index = 0;index < fruitArray.length; index++){
                    //拿水果名
                    fruitName = fruitArray[index]
                    //创建对应的节点
                    fruitNode = creatFruitNode(fruitName)
                    //添加水果节点
                    showNode.appendChild(fruitNode)
                }
                //点击确定按钮
                function addNewFruit(){
                    //拿到输入的水果名
                    var newFruitName = document.getElementById('newFruit').value
                    //创建对应的节点
                    var newFruitNode = creatFruitNode(newFruitName)
                    //设置创建的背景颜色
                    newFruitName.style.backgroundColor = randomColor()
                    //添加节点
                    showNode.insertBefore(newFruitName,showNode.firstElementChild)
                    
                    //清空输入框
                    document.getElementById('newFruit').value = ''
                }
                
            </script>
            
        </body>
    </html>
    
    image.png

    02缩略图案例

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <div>
                <img src="" alt="" id="big"/>
            </div>
            <div id="small"></div>
            
            <script type="text/javascript">
                //图片对象构造方法
                function YTImageModel(smallImage, bigImage, title=''){
                    this.smallImage = smallImage
                    this.bigImage = bigImage
                    this.title = title
                }
                
                imageArray = [
                    new YTImageModel('thumb-1.jpg', 'picture-1.jpg', '图片1'),
                    new YTImageModel('thumb-2.jpg', 'picture-2.jpg', '图片2'),
                    new YTImageModel('thumb-3.jpg', 'picture-3.jpg', 'iamge3'),
                ]
                //大图节点
                bigImgNode = document.getElementById('big')
                //得到图片的路径  .bigImage是调用方法
                bigImgNode.src = 'img/'+imageArray[0].bigImage
                //小图盒子
                smallBoxNode = document.getElementById('small')
                //创建小图标
                for(i=0; i<imageArray.length;i++){
                    //拿到图片模型
                    imageModel = imageArray[i]
                    //创建对应的小图节点
                    imageNode = document.createElement('img')
                    //设置图片
                    imageNode.src = 'img/'+ imageModel.smallImage
                    //***通过节点来保存图片模型
                    imageNode.imageMode = imageModel
                    //添加节点
                    smallBoxNode.appendChild(imageNode)
                    //添加事件
                    imageNode.addEventListener('mouseover', function(){
                        //使用鼠标点击的小图节点中保存的信息
                         bigImgNode.src = 'img/'+this.imageMode.bigImage
                         bigImgNode.title = this.imageMode.title
                    })
                }
                
                
                
                
            </script>
            
        </body>
    </html>
    
    image.png

    03闪烁效果案列

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script type="text/javascript" src="js/tool.js"></script>
            <style type="text/css">
                body{
                    text-align: center;
                }
                /*小盒子是60X60*/
                #box{
                    width: 600px;
                    height: 300px;
                    margin-left: auto;
                    margin-right: auto;
                    margin-bottom: 5px;
                    border: 1px solid #666666;
                }
                
                /*按钮*/
                button{
                    width: 60px;
                    height: 30px;
                    color: white;
                    background-color: red;
                    border: 0;
                    font-size: 16px;
                }
                button:focus{
                    outline: 0;
                }
                
                .newDiv{
                    width: 60px;
                    height: 60px;
                    float: left;
                }
                
            </style>
        </head>
        <body>
            <div id="box"></div>
            <button onclick="addAction()">添加</button>
            <button onclick="blink()" id="blink">闪烁</button>
            
            <script type="text/javascript">
            
                boxNode = document.getElementById('box')
                //添加
                function addAction(){
                    //创建节点
                    newDivNode = document.createElement('div')
                    newDivNode.className = 'newDiv'
                    newDivNode.style.backgroundColor = randomColor()
                    //添加节点
                    boxNode.insertBefore(newDivNode, boxNode.firstElementChild)
                    //判断是否溢出
                    if(boxNode.children.length > 50){
                        //删除最后一个
                        boxNode.lastElementChild.remove()
                    }
                }
                
                //闪烁
                function blink(){
                    //拿到按钮
                    blinkBtnNode = document.getElementById('blink')
                    //拿到所有的子标签
                    allSmallDiv = boxNode.children
        
                    if(blinkBtnNode.innerText == '闪烁'){
                        blinkBtnNode.innerText = '暂停'
                        //闪烁功能
                        timmer = setInterval(function(){
                            for(i=0; i<allSmallDiv.length;i++){
                                smallDivNode = allSmallDiv[i]
                                smallDivNode.style.backgroundColor = randomColor()
                            }
                        }, 100)
                        
                    }else{
                        blinkBtnNode.innerText = '闪烁'
                        //暂停功能
                        clearInterval(timmer)
                    }
                    
                }
            </script>
            
        </body>
    </html>
    
    image.png

    相关文章

      网友评论

          本文标题:2019-02-13JS应用

          本文链接:https://www.haomeiwen.com/subject/tcrjeqtx.html