原生前端练习

虽然原生的js css html 简单 但是万层高楼平地起,如果连基础都弄不明白又何去学vue react 等整合前端呢?
响应式:
效果图:
在这里插入图片描述
在这里插入图片描述

代码:

<!DOCTYPE html>
<html lang="cn" xmlns:c="http://www.w3.org/1999/XSL/Transform">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,user-scalable=yes,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
    <title>调色盘</title>
    <script src="/js/jquery-3.5.1.js"></script>
    <link rel="stylesheet" href="/myfont/iconfont.css">
    <link rel="stylesheet" href="/css/color.css">
    <script src="/js/color.js"></script>
</head>
<body>

<div class="parent">
    <div class="search">
        <div class="searchForm">
            <input type="text" id="searchText" placeholder="颜色名字或16进制"><button id="btn" class="btn iconfont"></button>
        </div>

        <div class="colorPinkDiv">
            <input type="color" class="colorPink" value="#2aabd2"><label>点击此处挑选颜色</label>
        </div>

        <p style="text-align: left;text-indent: 1em;padding: 0 3px">挑选你喜欢的颜色吧!
        如果找不到你的颜色,或你有其他好看样式,或者里面样式有错,请在下方留言吧!</p>
        <div style="width: 100%;height: 90px;position: relative;">
            <textarea class="liuYan"></textarea>
            <button id="sub" class="sub iconfont" style="border: none;position: absolute;top:10px;right: 3px;font-size: 18px"></button>
        </div>
<script>

</script>

    </div>
    <div class="colorContent"></div>
</div>
</body>
</html>

css:

* {
    background-color: transparent;
    margin: 0;
    padding: 0;
}
div {
    border: 1px solid hsla(0,0%,100%,0);
    border-radius: 2px;
}

@media screen and (max-width: 767px){
    .parent {
        width: 100%;
    }
    .colorContent {
        width: 90%;
    }
    .colorDiv {
        width: 100%;
    }
    .search {
        width: 70%;
    }
}
@media screen and (min-width: 768px){
    .parent {
        width: 750px;
    }
    .colorContent {
        /*width: 604px;*/
        width: 90%;
    }
    .search {
        width: 550px;
    }
}
@media screen and (min-width: 992px){
    .parent {
        width: 970px;
    }
    .colorContent {
        width: 730px;
    }
    .search {
        width: 230px;
    }

}
@media screen and (min-width: 1200px){
    .parent {
        width: 1170px;
    }
    .colorContent {
        width: 920px;
    }
    .search {
        width: 240px;
    }
}

.parent {
    margin: 0 auto;
    height: 100%;
    text-align: center;
    /*background-image: url("/texture.png");*/
    background-color: #dddddd;
    transition: background-color 1.5s ease 0.5s;
}
.colorDiv {
    margin: 10px 0;
    padding: 0;
    width: 300px;
    height: 40px;
    text-align: center;
}
.searchColorDiv {
    margin: 3px 0;
    padding: 0;
    width: 100%;
    height: 40px;
    text-align: center;
}

.search {
    display: inline-block;
    position: relative;
    top: 10px;
    vertical-align: top;
    height: 100%;
    background-color: #a7a8bd;

}
.colorContent {
    position: relative;
    top: 0px;
    vertical-align: top;
    text-align: center;
    /*background-color: #8c4356;*/
    background-color: transparent;
    display: inline-block;
}
.colorDivs{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content: space-around;
}
.color {
    display: inline-block;
    width: 33.4%;
    height: 40px;
    margin: 0 3px;
    background-color: #f0a1a8;
}

.colorName {
    height: 40px;
    width: 16.6%;
    display: inline-block;
    background-color: #fff;
    vertical-align: top;
    line-height: 40px;

}
.colorNum {
    height: 40px;
    display: inline-block;
    background-color: #fff;
    vertical-align: top;
    line-height: 40px;
    width: 30%;
}
.copy {
    height: 40px;
    display: inline-block;
    vertical-align: top;
    background-color: #fff;

}
.copyBtn {
    display: none;
    height: 40px;
    border: none;
}
.copyBtn:before {
    content: '\e701';
}
.colorNum:hover .copyBtn{
    display: inline-block;
}

.searchForm {
    display: block;
    border: 1px solid hsla(0,0%,100%,0);
    border-radius: 2px;
    border: none;
    background-color: #fff;
    margin: 4px;
    height: 27px;
}
#searchText {
    height: 100%;
    width: 80%;
    border: none;
    outline: none;
}
.btn {
    height: 100%;
    border: none;
    width: 20%;
    background-color: #a7a8bd;

}
.btn:before {
    content: "\e60a";
}
.sub:before{
    content: "\e624";
}

.liuYan {
    border: 1px solid hsla(0,0%,100%,0);
    border-radius: 2px;
    width: 96%;
    margin-top: 5px;
    outline: none;
    height: 80px;
    background-color: white;
}
.colorPinkDiv {
    /*以下用于label居中*/
    margin-left: 3px;
    align-items: center;
    justify-content: start;
    display: flex;/*兼容IE*/
    display: -webkit-flex;
}
.colorPink {
    width: 50px;
    border: 1px solid hsla(0,0%,100%,0);
    border-radius: 2px;
    height: 27px;
}

js:

$(function () {
    var parent = document.querySelector('.parent');
    var colorContent = document.querySelector('.colorContent');
    var colorDivs = document.createElement('div');
    colorDivs.className = 'colorDivs';
    colorContent.appendChild(colorDivs);
    var colors;
    //加载数据
    $.ajax({
        //接口地址
        type: "get",//请求方式
        url: "/json/colors.json",//地址,就是json文件的请求路径
        dataType: "json",
        success: function (data) {
            colors = data;
            for (var i=0;i<data.length;i++){

                var colorDiv = document.createElement('div')
                colorDiv.className = 'colorDiv';
                colorDivs.appendChild(colorDiv);

                var colorName = document.createElement('div');
                colorName.className = 'colorName';
                colorName.innerText = data[i]['name'];
                colorDiv.appendChild(colorName);

                var color = document.createElement('div');
                color.className = 'color';
                color.style.backgroundColor = data[i]['value'];
                colorDiv.appendChild(color);

                color.addEventListener('mouseover',function () {
                    parent.style.backgroundColor = this.style.backgroundColor;
                })
                color.addEventListener('mouseout',function () {
                    parent.style.backgroundColor = '#dddddd';
                })

                var colorNum = document.createElement('div');
                colorNum.className = 'colorNum';
                colorNum.innerText =  data[i]['value'];
                colorDiv.appendChild(colorNum);

                var copyBtn = document.createElement('button');
                copyBtn.className = 'copyBtn iconfont';
                colorNum.appendChild(copyBtn);
                copyBtn.addEventListener('click',function () {
                    MyCopy(this.parentNode.parentNode.children[2].innerText);
                });
            }
        },
        error: function (returndata) {
            alert('网络异常稍后再试');
        }
    });
    //搜索
    var btn = document.getElementById('btn');
    btn.addEventListener('click',function (){
        var text = document.getElementById('searchText');
        var data = searchColor(text.value,colors);
        var search = document.querySelector('.search');
        var old = document.querySelector('.searchColorDiv')
        if(old!=null){
            search.removeChild(old);
        }

        var colorDiv = document.createElement('div')
        colorDiv.className = 'searchColorDiv';
        search.appendChild(colorDiv);
        for (var i=0;i<data.length;i++){
            var colorName = document.createElement('div');
            colorName.className = 'colorName';
            colorName.innerText = data[i]['name'];
            colorName.style.width='52px';
            colorDiv.appendChild(colorName);

            var color = document.createElement('div');
            color.className = 'color';
            color.style.backgroundColor = data[i]['value'];
            color.style.width='30%';
            colorDiv.appendChild(color);

            color.addEventListener('mouseover',function () {
                parent.style.backgroundColor = this.style.backgroundColor;
            })
            color.addEventListener('mouseout',function () {
                parent.style.backgroundColor = '#dddddd';
            })

            var colorNum = document.createElement('div');
            colorNum.className = 'colorNum';
            colorNum.innerText =  data[i]['value'];
            colorNum.style.width =  '90px';
            colorDiv.appendChild(colorNum);

            var copyBtn = document.createElement('button');
            copyBtn.className = 'copyBtn iconfont';
            colorNum.appendChild(copyBtn);
            copyBtn.addEventListener('click',function () {
                MyCopy(this.parentNode.parentNode.children[2].innerText);
            });
        }
        return false;
    })

    //留言
        var sub = document.getElementById('sub');
        sub.addEventListener('click',function (){
            var textarea = document.querySelector('.liuYan');
            if(textarea.value.length==0){
                alert('请输入内容!');
            }else {
                $.ajax({
                    url: '/liuYan',
                    data:{
                        'liuYan':textarea.value,
                    },
                    success:function (data){
                        alert(data);
                    }
                });
            }
            return false;
        });

});


function MyCopy(content) {
    const input = document.createElement('input')
    document.body.appendChild(input)
    input.setAttribute('value', content)   //内容
    input.select()
    if (document.execCommand('copy')) {
        document.execCommand('copy')
    }
    document.body.removeChild(input)
    alert('复制成功');
}

function searchColor(color,colors) {
    var list = [];
    console.log(color);
    for (var i=0;i<colors.length;i++){
        if (colors[i]['name']==color){
            list.push(colors[i])
        }
        if (colors[i]['value']==color){
            list.push(colors[i])
        }
    }
    return list;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值