代码拉取完成,页面将自动刷新
同步操作将从 doudoucode/javascript_design_patterns 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.boxs{
display: flex;
flex-direction: row;
}
.box{
margin: 20px;
width:100px;
height:100px;
background: #999;
text-align: center;
font-size: 2rem;
line-height: 100px;
transition: all 0.5s ease-in-out;
user-select: none;
cursor: pointer;
border-radius: 100%;
box-shadow: 2px 5px 60px #ddd;
}
.active{background:red}
</style>
<body>
<div class="boxs">
<div class="box">高</div>
<div class="box">富</div>
<div class="box">帅</div>
</div>
<script>
class Client{
constructor(nodes,strategy){
this.nodes = [...nodes];
this.strategy = strategy;
this.initEvent();
}
initEvent(){
this.nodes.forEach(item=>{
item.addEventListener("click",(e)=>{
this.handleClick(e.target);
})
})
}
handleClick(ele){
this.strategy.process(ele,this.nodes);
}
}
class Strategy1{
process(ele,nodes){
ele.classList.toggle("active");
}
}
class Strategy2{
process(ele,nodes){
const items = nodes.filter(item=>item.classList.contains("active"));
if(items.length===2 && !items.includes(ele))
return;
ele.classList.toggle("active");
}
}
class Strategy3{
process(ele,nodes){
ele.classList.toggle("active");
if(nodes.every(item=>item.classList.contains("active"))){
nodes.filter(item=>item!==ele)[Math.floor(Math.random()*2)]
.classList.toggle("active");
}
}
}
const client = new Client(document.querySelectorAll(".box"),new Strategy3());
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。