JS小方块 随颜色变化宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
#box{
width: 200px;
height: 200px;
background: black;
margin: 0 auto;
}
#box1{
font-size: 30px;
width: 200px;
text-align: center;
margin: 0 auto;
}
.sss {
color: orange;
}
</style>
</head>
<body>
<div id="box"></div>
<div id = "box1" style="background:green" class="xxx"> 123</div>
<script>
let div = document.getElementById("box");
console.dir(div);
div.style.background='black';
div.style.width='200px';
div.style.height='200px';
div.onclick = function(){
let color = div.style.background;
switch(color){
case 'black':
div.style.background = 'orange';
break;
case 'orange':
div.style.background = 'green';
break;
case 'green':
div.style.background = 'red';
break;
default:
div.style.background = 'black'
}
let width = div.style.width;
switch(width){
case '200px':
div.style.width = '150px';
break;
case '150px':
div.style.width = '100px';
break;
case '100px':
div.style.width = '160px';
break;
default:
div.style.width = '200px';
}
let height = div.style.height;
switch(height){
case '200px':
div.style.height = '150px';
break;
case '150px':
div.style.height = '100px';
break;
case '100px' :
div.style.height= '160px';
break;
default:
div.style.height = '200px'
}
}
console.log(div);
</script>
</body>
</html>
JS简单点击隐藏显示文字图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>隐藏显示文字图片</title>
<style>
*{
margin:0;
padding:0;
}
#box{
width: 500px;
margin: 0 auto;
text-align: center;
background: chartreuse;
}
#box p{
font-size: 50px;
color: orange;
cursor: pointer;
}
#box img{
display: block;
width: 500px;
}
#box .aabb{
display: none;
}
</style>
</head>
<body>
<!-- 一,隐藏显示图片文字 -->
<div id="box">
<p id="btn">隐藏</p>
<img id="people" src="404.jpg" alt="">
</div>
<script>
let btn = document.getElementById('btn');
let people = document .getElementById('people');
console.dir(btn);
console.dir(people);
btn.onclick=function(){
let value = btn.innerText;
if(value === '隐藏'){
btn.innerText = '显示'
people.style.display='none'
}else{
btn.innerText = '隐藏'
people.style.display='block'
}
}
</script>
</body>
</html>
JS 简单隔行变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>隔行变色</title>
<style>
*{
margin: 0;
padding:0;
}
ul{
list-style: none;
}
#box{
width:500px;
margin: 20px auto;
text-align: center;
}
li{
font-size: 20px;
color: chartreuse;
line-height: 30px;
border: 1px solid blue;
}
</style>
<!-- 设置基本样式 -->
</head>
<body>
<div id ="box">
<ul id ="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
<script>
let ul = document.getElementById('list')
let list = ul.getElementsByTagName('li')
let color = null;
console.log(list);
for(var i=0;i<list.length;i++){
console.log(list[i])
if(i%2 === 0){
list[i].style.background = 'black';
}else{
list[i].style.background = 'yellow';
}
list[i].onmouseenter = function(){
color = this.style.background;
this.style.background = 'blue';
}
list[i].onmouseout = function(){
this.style.background = color;
}
}
</script>
</body>
</html>
JS 简单选项卡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 530px;
margin: 20px auto;
border: 1px solid black;
}
#list {
list-style: none;
display: flex;
}
li {
width:105px;
font-size: 20px;
text-align: center;
line-height: 50px;
color: orange;
cursor: pointer;
border-right: 1px solid black;
}
#box div {
width: 531px;
height: 300px;
display: none;
}
#box div img{
width:530px;
}
#box div.active{
display: block;
}
li.active{
border-bottom-color:white;
background:#87CEEB;
}
</style>
</head>
<body>
<div id="box">
<ul id="list">
<li class="active">空之律者1</li>
<li>空之律者2</li>
<li>空之律者3</li>
<li>空之律者4</li>
<li>空之律者5</li>
</ul>
<div class="active">
<img src="5.jpg" alt="">
</div>
<div>
<img src="9.jpg" alt="">
</div>
<div>
<img src="1.jpg" alt="">
</div>
<div>
<img src="8.jpg" alt="">
</div>
<div>
<img src="3.jpg" alt="">
</div>
</div>
<script>
let box = document.getElementById('box');
let nav = document.getElementById('list');
let list = nav.getElementsByTagName('li');
let div = box.getElementsByTagName('div');
console.log(list,div);
for(var i = 0;i<list.length;i++){
list[i].aa = i;
list[i].onclick = function(){
console.log(this.aa);
fn(this.aa);
}
}
function fn(index){
console.dir()
for(var i=0; i<list.length;i++){
list[i].className= '';
div[i].className= '';
}
list[index].className= 'active';
div[index].className= 'active';
}
</script>
</body>
</html>
JS 简单定时器 切换图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 600px;
height: 300px;
margin: 0 auto;
}
#time {
width: 600px;
height: 300px;
text-align: center;
line-height: 300px;
color: orange;
background: rgba(0, 255, 179, 0.63);
font-size: 50px;
position: relative;
display: none;
transition: all 1s;
}
#box img {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div id="box">
<div id="time">时间到</div>
<div id="aa"><img src="1.jpg" alt=""></div>
</div>
<script>
let box = document.getElementById('box')
let aa = document.getElementById('aa')
let img = aa.getElementsByTagName('img')
let time = document.getElementById('time')
console.log(box,aa,time);
console.log(img[0].src)
let flag = 1;
function fn() {
switch (img[0].src) {
case '图片地址':
img[0].src = '图片地址';
break;
case '图片地址':
img[0].src = '图片地址';
break;
case '图片地址':
img[0].src = '图片地址';
break;
case '图片地址':
img[0].src = '图片地址';
break;
default:
img[0].src = '图片地址';
}
flag++;
if(flag>20){
clearInterval(int);
img[0].src ="";
time.style.display="block";
}
}
let int = setInterval(fn, 500)
</script>
</body>
</html>