今天主要记录5个js原生例子,通过用前面所学完成,提高逻辑和独立思考的能力。
方法一通百通,后期会随时添加新的方法。
1、添加标签至文本框,并可删除
主要运用了DOM的基本操作
备注:批量添加事件用循环;
循环里的点击事件用this获取他关联的元素;
给标签添加的文字内容里也可再加标签,用字符串或模板字符串。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
ul li {
list-style: none;
color: #fff;
padding: 10px 0;
}
ul li >button {
background-color: #F25D4D;
color: #fff;
padding: 5px 10px;
margin-left: 200px;
}
.box {
background-color: #002D52;
width: 500px;
height: 560px;
margin: 20px auto;
padding: 8px 20px;
}
.contain {
margin: 10px 20px;
}
p{
color: #fff;
line-height: 30px;
margin: 4px;
display: inline-block;
text-align: center;
}
.op11 {
padding-bottom: 10px;
}
.btn-all {
background-color: #F25D4D;
color: #fff;
padding: 5px 30px;
margin-top: 5px;
}
.bg {
background-color: #349851;
border: 1px solid #999;
width: 100px;
height: 30px;
}
.text-bg {
background-color: #ffffff;
width: 400px;
height: 150px;
padding: 5px;
overflow-y: auto;
}
</style>
</head>
<body>
<div class="box">
<div class="contain">
<p class="op11">单击下面的”添加“按钮,添加标签</p>
<div>
<div id="bgBox" class="text-bg"></div>
</div>
<button class="btn-all">全部清除</button>
<ul>
<li><span>John Doe1</span> <button>添加</button></li>
<li><span>John Doe2</span> <button>添加</button></li>
<li><span>John Doe3</span> <button>添加</button></li>
<li><span>John Doe4</span> <button>添加</button></li>
<li><span>John Doe5</span> <button>添加</button></li>
<li><span>John Doe6</span> <button>添加</button></li>
</ul>
</div>
</div>
<script>
/*
点击全部清除:清空文本框内容
点击添加,生成对应文字内容的按钮至上方文本框
点击事件发生
创造绿色按钮元素
为该元素提供下方按钮对应文字
用按钮长度循环,其中点击事件:点击某按钮,取其父级文字
同时上方文本框生成按钮
生成的按钮是以文本框作为父级添加子元素+文本内容
*/
var oUl = document.querySelector('ul');
var oBtn = document.querySelectorAll('button');
var bgBox = document.getElementById('bgBox');
oBtn[0].onclick = function (){
bgBox.innerHTML='';
}
// 给每个按钮添加事件 循环
for (var i=1; i<oBtn.length;i++){
oBtn[i].onclick = function (){
var oLi = this.parentNode;
var str = oLi.children[0].innerText;
console.log('str',str)
var add = document.createElement('p');
add.innerHTML=str + '<a>x</a>';
add.classList.add('bg')
bgBox.appendChild(add)
var oA = add.children[0];
oA.onclick = function (){
bgBox.removeChild(add)
}
}
}
</script>
</body>
</html>
2、数组去重,统计
方法:1、json:json的key不重复
2、indexOf
3、indexOf和lastindexOf
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<textarea id="t1" cols="50" rows="10">衣服 运动鞋 裙子 裤子 衣服 运动鞋 裙子 裤子 衣服 运动鞋 裙子 裤子 衣服 运动鞋 衣服 运动鞋 衣服 运动鞋 裙子 衣服 运动鞋 裙子</textarea>
<button id="btn">转化</button>
<textarea cols="50" rows="10" id="t2"></textarea>
<script>
var oBtn = document.getElementById('btn');
var oT1 = document.getElementById('t1');
var oT2 = document.getElementById('t2');
oBtn.onclick = function (){
// 获取t1的内容
var str = oT1.value;
// 把获取的内容切割成数组
var arr = str.split(' ');
// 准备一个json
var json = {};
// 循环数组
for(var i=0;i<arr.length;i++){
if ( json[arr[i]]===undefined){
json[arr[i]]=1
}else {
json[arr[i]]+=1
}
}
console.log(json)
var result = '';
for(var key in json){
result+=key+'('+json[key]+')'
}
oT2.value=result
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<textarea cols="50" rows="10">衣服 运动鞋 裙子 裤子 衣服 运动鞋 裙子 裤子 衣服 运动鞋 裙子 裤子 衣服 运动鞋 衣服 运动鞋 衣服 运动鞋 裙子 衣服 运动鞋 裙子</textarea>
<button>转化</button>
<textarea cols="50" rows="10"></textarea>
<script>
// 获取元素
var oBtn = document.querySelector('button');
var inPutValue = document.querySelectorAll('textarea');
var str = inPutValue[0].innerHTML;
var arr = str.split(' ');
//方法1:设一个空新数组,循环获取数组每一项,如果新数组中通过indeOf找不到的数组元素,则push进新数组
var newArr = [];
for (var i=0; i<arr.length; i++){
if (newArr.indexOf(arr[i]) === -1){newArr.push(arr[i]);}
}
console.log(newArr)
/*
// 方法2:如果他们不相等,说明是重复的元素,在数组中删掉他,同时i--
for (var i=0; i<arr.length; i++){
if (arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])) {
arr.splice(i, 1);
i--;
}
}
console.log(arr);*/
oBtn.onclick = function () {
inPutValue[0].innerHTML='';
inPutValue[1].innerHTML=newArr.join(' ');
}
</script>
</body>
</html>
3、侧边菜单栏展开收起
备注:主要用到了getComputedStyle和offsetHeight
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
#nav{
width: 200px;
background-color: #ccc;
}
h6{
font-size: 20px;
line-height: 40px;
}
#nav ul{
height: 0;
overflow: hidden;
transition: .3s height linear;
}
</style>
</head>
<body>
<ul id="nav">
<li>
<h6>一级目录1</h6>
<ul>
<li>二级目录1-1</li>
<li>二级目录1-2</li>
<li>二级目录1-3</li>
</ul>
</li>
<li>
<h6>一级目录2</h6>
<ul>
<li>二级目录2-1</li>
<li>二级目录2-2</li>
<li>二级目录2-3</li>
</ul>
</li>
<li>
<h6>一级目录3</h6>
<ul>
<li>二级目录3-1</li>
<li>二级目录3-2</li>
<li>二级目录3-3</li>
</ul>
</li>
</ul>
<script>
var aH6 = document.querySelectorAll('#nav h6');
var aUl = document.querySelectorAll('#nav ul');
for(var i=0;i<aH6.length;i++){
aH6[i].onclick = function (){
//li的高度*li的个数
var oUl = this.nextElementSibling;
if(getComputedStyle(oUl).height==='0px'){
// 让其他 aUl[i].style.height='0px',这样一次点击,其他的都会合上,而不是一直展开着
for (var i=0;i<aUl.length;i++){
aUl[i].style.height='0px'
}
// 本次点击的ul的高度=li的高度*li的个数
oUl.style.height=oUl.children[0].offsetHeight*oUl.children.length+'px';
}else {
// 如果点击的一级标题是展开的,点击后就设为合上
oUl.style.height='0px'
}
}
}
</script>
</body>
</html>
4、列表渲染 — 字符串拼接、模板字符串
备注:注意字符串拼接、模板字符串的区别
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul{
width: 1000px;
border: 1px solid #ccc;
}
*{
margin: 0;
padding: 0;
}
.clearfix:after{
content:'';
display: block;
clear: both;
}
li {
width: 300px;
height: 200px;
margin: 5px 16px;
float: left;
list-style: none;
background-color: #ccc;
}
li div {
float: left;
margin: 30px 0;
}
li div img {
width: 150px;
}
</style>
</head>
<body>
<ul class="clearfix">
<!--<li>
<div>
<img src="" alt="">
</div>
<div>
<p>aaa</p>
<p>¥1200</p>
</div>
</li>-->
</ul>
<script>
var goodsDate = [
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '13' },
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '14' },
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '15' },
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '16' },
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '17' },
{ src: 'https://gw.alicdn.com/bao/uploaded/i2/504369715/O1CN01VDTglP2LdWUZPQ849_!!0-item_pic.jpg_300x300q90.jpg_.webp', tit: 'aaa', price: '18' },
];
var oUl = document.querySelector('ul');
/*// 字符串方法
for(var i=0; i<goodsDate.length;i++){
var oLi = document.createElement('li');
oLi.innerHTML = '<div> <img src="'+ goodsDate[i].src+'" alt=""> </div><div><p>' + goodsDate[i].tit + '</p><p>'+ goodsDate[i].price+'</p></div>';
oUl.appendChild(oLi);
}*/
// 模板字符串方法
for(var i=0;i<goodsDate.length;i++){
var oLi = document.createElement('li');
oLi.innerHTML=`
<div>
<img src="${goodsDate[i].src}" alt="">
</div>
<div>
<p>${goodsDate[i].tit}</p>
<p>${goodsDate[i].price}</p>
</div>
`
oUl.appendChild(oLi)
}
/* 注意:
(1)字符串:
'helle world' 如果遇到了字符串拼接 ’hello ‘+ 变量 +' world'
字符串里面即使内容再多也不能换行写
(2)模板字符串:
在es6里出的新的写法 `hello ${变量} world`
里面随便换行
简单的字符串可以用''
需要复杂的换行,或者想简单一点写变量用 `hello ${变量} world`
*/
</script>
</body>
</html>
5、轮播图 — 淡入淡出效果
备注:
相同的内容封装函数以提高代码复用;
批量添加事件用循环;
循环里的点击事件用this获取他关联的元素;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 600px;
height: 400px;
margin: 40px auto;
position: relative;
}
ul{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top:0;
border: 1px solid #ccc;
}
ul li{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top:0;
opacity:0;
transition: .2s all linear;
}
li{
list-style: none;
}
ul .active{
opacity: 1;
}
ol{
position: absolute;
width: 300px;
left: 150px;
bottom:20px;
}
ol li{
width: 30px;
height: 30px;
border: 5px solid #999;
margin: 10px;
float: left;
}
ol .active{
background-color: #eee;
}
.left{
position: absolute;
left: 20px;
top:180px;
font-size: 40px;
}
.right{
position: absolute;
right: 20px;
top: 180px;
font-size: 40px;
}
</style>
</head>
<body>
<div>
<ul>
<li class="active"><img src="./img/1.jpeg" alt=""></li>
<li><img src="./img/2.jpeg" alt=""></li>
<li><img src="./img/3.jpeg" alt=""></li>
<li><img src="./img/4.jpeg" alt=""></li>
<li><img src="./img/5.jpeg" alt=""></li>
</ul>
<a class="left"> < </a>
<a class="right"> > </a>
<ol>
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<script>
var oBox = document.querySelector('div');
var imgLi = document.querySelectorAll('ul li');
var btnLi = document.querySelectorAll('ol li');
var oRight = document.querySelector('.right');
var oLeft = document.querySelector('.left');
var iNow = 0;// 默认现在显示的下标
function move(){
for (var i=0;i<btnLi.length;i++){
btnLi[i].classList.remove('active');
imgLi[i].classList.remove('active');
}
imgLi[iNow].classList.add('active');
btnLi[iNow].classList.add('active');
}
function toRight(){
iNow++;
if(iNow==imgLi.length){
iNow=0;
}
move()
}
// 给所有的按钮添加点击事件
for(var i=0; i<btnLi.length;i++){
btnLi[i].dataset.index=i;//取出下标
btnLi[i].onclick=function (){
iNow=this.dataset.index;
move();
}
}
// 给右边的按钮加点击事件
oRight.onclick = toRight;
// 给左边的按钮加点击事件
oLeft.onclick = function (){
iNow--;
if(iNow===-1){
iNow=imgLi.length-1
}
move()
}
// 定时器
var timer = setInterval(toRight,2000);
// 鼠标移入
oBox.onmouseover = function (){
clearInterval(timer)
}
// 鼠标移出
oBox.onmouseout=function (){
timer=setInterval(toRight,2000)
}
</script>
</body>
</html>