本次是轮播图的简单实现,往后会更新 最终版 补充昨天的snake:
function SnakeGame({position="relative", screenWidth=200, screenHeight=200, border="5px solid lightblue", margin="0" } = {}) {
this.snake = [];
this.spWidth = 20 ;
this.screenWidth = screenWidth ;
this.screenHeight = screenHeight ;
this.rows = this.screenHeight / this.spWidth;
this.cols = this.screenWidth / this.spWidth;
this.playing = false ;
this.position = position;
this.margin = margin ;
this.border = border ;
}
SnakeGame.prototype = {
initGame() {
this.screen = document.createElement("div");
this.screen.style.position = this.position ;
this.screen.style.width = `${this.screenWidth}px`;
this.screen.style.height = `${this.screenHeight}px`;
this.screen.style.border = this.border ;
this.screen.style.margin = this.margin ;
this.screen.style.fontSize = "0";
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
let span = document.createElement("span");
span.style.display = "inline-block";
span.style.width = `${this.spWidth - 1}px`;
span.style.height = `${this.spWidth - 1}px`;
span.style.borderRight = "1px solid #ddd"
span.style.borderBottom = "1px solid #ddd"
if (i == 0 && j < 3) {
span.classList.add("sk")
}
this.screen.append(span);
}
}
for (let i = 0; i < 3; i++) {
let p = document.createElement("p");
p.style.position = "absolute";
p.style.width = "20px";
p.style.height = "20px";
p.style.background = "#000";
p.style.top = 0;
p.style.left = i * 20 + "px";
this.snake.unshift(p);
this.screen.appendChild(p);
}
this.randomFood();
this.spans = this.screen.querySelectorAll("span")
document.body.appendChild(this.screen);
},
randomFood() {
let spans = this.screen.querySelectorAll("span:not(.sk)");
let index = Math.floor(Math.random() * spans.length)
let span = spans[index];
let tag = document.createElement("b");
tag.style.display = "block";
tag.style.width = "10px"
tag.style.height = "10px";
tag.style.margin = "5px";
tag.style.backgroundColor = "#f00"
tag.style.borderRadius = "50%"
span.appendChild(tag);
},
start() {
this.initGame();
document.addEventListener("keydown", (event) => {
if (event.keyCode == 32 && !this.playing) {
this.dir = "right";
this.playing = setInterval(()=>{
let sk = this.snake[0];
let left = sk.offsetLeft;
let tops = sk.offsetTop;
if (this.dir === "right") {
left += 20;
} else if (this.dir === "left") {
left -= 20;
} else if (this.dir === "top") {
tops -= 20;
} else if (this.dir === "down") {
tops += 20;
}
this.addSnake(left, tops)
}, 300)
} else if(this.playing && event.keyCode == 37 && this.dir !="right"){
this.dir = "left";
}else if(this.playing && event.keyCode == 38 && this.dir !="down"){
this.dir = "top";
}else if(this.playing && event.keyCode == 39 && this.dir !="left"){
this.dir = "right";
}else if(this.playing && event.keyCode == 40 && this.dir !="top"){
this.dir = "down";
}
})
},
getIndex(left, tops) {
let x = left/this.spWidth ;
let y = tops/this.spWidth;
return y * this.cols + x ;
},
gameOver() {
alert("/(ㄒoㄒ)/~~GAME OVER!!!");
clearInterval(this.playing);
this.screen.remove();
this.snake = [] ;
this.playing = false ;
this.dir = "right";
this.initGame() ;
},
addSnake(left, tops) {
if (left >= this.screen.clientWidth || left <0 || tops < 0 || tops >= this.screen.clientHeight) {
this.gameOver();
return ;
}
let p = document.createElement("p");
p.style.position = "absolute";
p.style.width = `${this.spWidth}px`;
p.style.height = `${this.spWidth}px`;
p.style.background = "#000";
p.style.top = tops + "px";
p.style.left = left + "px";
let index = this.getIndex(left, tops)
let span = this.spans[index];
let b = span.querySelector("b")
if (b ==null) {
let p2 = this.snake.pop()
left = p2.offsetLeft ;
tops = p2.offsetTop ;
index = this.getIndex(left, tops)
let span2 = this.spans[index];
span2.classList.remove("sk")
p2.remove();
}
if (span.classList.contains("sk")) {
this.gameOver();
return ;
}
span.classList.add("sk")
this.snake.unshift(p);
this.screen.appendChild(p);
if(b !=null) {
b.remove();
this.randomFood();
}
}
}
js-轮播图的实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none
}
.swiper {
position: relative;
width: 960px;
height: 460px;
margin: 50px auto 0;
}
.swiper img {
position: absolute;
width: 100%;
height: 100%;
display: none;
}
.swiper img:first-child {
display: block;
}
.swiper .dots {
position: absolute;
height: 40px;
width: 100%;
background-color: rgba(0, 0, 0, 0.5);
bottom: 0px;
text-align: center;
font-size: 0;
}
.swiper .dots span {
display: inline-block;
width: 10px;
height: 10px;
background-color: snow;
margin: 15px 5px;
border-radius: 50%;
}
.swiper .dots .cur {
background-color: red;
}
.swiper .left, .swiper .right {
position: absolute;
width: 40px;
height: 70px;
background: url("./images/icon-slides.png") no-repeat;
top: 210px;
cursor: pointer;
}
.swiper .right {
right: 0px;
background-position: -43px 0;
}
.swiper .left:hover {
background-position: -85px 0;
}
.swiper .right:hover {
background-position: -128px 0;
}
@keyframes fadeOut {
to {
transform: translateX(-960px);
height: 100px;
margin: 150px 0;
opacity: 0;
}
}
@keyframes fadeIn {
from {
transform: translateX(-960px);
height: 0px;
width: 0px;
opacity: 0;
}
to {
transform: translateX(0);
height: 400px;
width: 960px;
opacity: 1;
}
}
</style>
</head>
<body>
<div class="swiper">
<img class="cur" src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/a532e33470d046b3f044d5ea49fc5e9e.png?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/918820682e4a490221cfd92b24c14b86.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/67c7222fbcf98db942718eaf29f32297.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/01a5d7313fe344bddb9e4081df4dc998.jpg?w=2452&h=920"/>
<div class="dots">
<span class="cur"></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
<script>
function showImg(index) {
let img = document.querySelector(".swiper > .cur");
let target = document.querySelector(`.swiper img:nth-of-type(${index + 1})`);
target.style.animation = "fadeIn 1s";
img.style.animation = "fadeOut 1s" ;
target.style.display = "block";
target.addEventListener("animationend",function (){
target.removeAttribute("style");
target.style.display = "block" ;
target.classList.add("cur");
})
img.addEventListener("animationend",function (){
img.removeAttribute("style");
img.style.display = "none";
img.classList.remove("cur");
})
document.querySelector(".swiper .dots .cur").classList.remove("cur")
document.querySelector(`.swiper .dots span:nth-of-type(${index + 1})`).classList.add("cur")
}
let index = 0;
let len = document.querySelectorAll(".swiper img").length;
document.querySelector(".swiper .right").addEventListener("click", function (event) {
if (index == len - 1) {
index = -1;
}
showImg(++index);
})
document.querySelector(".swiper .left").addEventListener("click", function (event) {
if (index == 0) {
index = len;
}
showImg(--index);
})
let dots = document.querySelectorAll(".swiper .dots span")
document.querySelector(".swiper .dots").addEventListener("click", function (event) {
if (event.target.tagName == "SPAN") {
index = Array.from(dots).findIndex(s => s === event.target)
showImg(index);
}
})
let playing = false;
function autoPlay() {
playing = setInterval(function () {
document.querySelector(".swiper .right").dispatchEvent(new MouseEvent("click"));
}, 2000);
}
function stopPlay(){
clearInterval(playing);
}
document.querySelector(".swiper").addEventListener("mouseover",stopPlay);
document.querySelector(".swiper").addEventListener("mouseout",autoPlay);
autoPlay();
</script>
</body>
</html>
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
js-全选效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="checkbox" name="" id="all">全选
<hr/>
<div id="hobby">
<input type="checkbox" name="" value="游泳">游泳
<input type="checkbox" name="" value="爬山">爬山
<input type="checkbox" name="" value="学JAVA">学JAVA
<input type="checkbox" name="" value="旅游">旅游
</div>
<p>
您选择的爱好有:<b id="res">[]</b>
</p>
<script>
let hobbies = [];
document.querySelector("#all").addEventListener("change", function () {
let tags = document.querySelectorAll("#hobby input[type = checkbox]");
for (let tag of tags) {
tag.checked = this.checked;
if (this.checked) {
hobbies.push(tag.value);
}
}
if (this.checked) {
document.querySelector("#res").innerText = `[${hobbies}]`;
} else {
hobbies = [];
document.querySelector("#res").innerText = `[]`;
}
})
document.querySelector("#hobby").addEventListener("change", function (event) {
if (event.target.nodeName === "INPUT") {
let len = this.querySelectorAll(":checked").length;
let ck = len === this.children.length;
document.querySelector("#all").checked = ck;
let status = event.target.checked;
if (status) {
hobbies.push(event.target.value);
} else {
let index = hobbies.indexOf(event.target.value);
hobbies.splice(index, 1);
}
document.querySelector("#res").innerText = `[${hobbies}]`;
}
})
</script>
</body>
</html>
js-阻止元素的默认行为
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function goQQ() {
window.location.href = "https://www.qq.com"
return false ;
}
</script>
</head>
<body>
<a href="https://www.baidu.com" onclick="return goQQ()">百度</a>
<a href="https://www.sina.com" id="sina">新浪</a>
<script>
document.querySelector("#sina").addEventListener("click", function(event){
window.location.href = "https://www.baidu.com";
event.preventDefault() ;
})
</script>
</body>
</html>
js-滚动轮播图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#swiper {
position: relative;
width: 960px;
height:400px;
margin:100px auto 0 ;
font-size: 0;
}
#swiper .images {
position: absolute;
width: 100%;
height:100%;
white-space: nowrap;
overflow: hidden;
}
#swiper img {
width: 100%;
height: 100%;
}
#swiper .dots {
position: absolute;
height: 40px;
bottom: 0px;
background-color: rgba(0, 0, 0, 0.6);
width: 100%;
font-size: 0;
text-align: center;
}
#swiper .dots span {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
margin: 15px 5px;
cursor: pointer;
}
#swiper .dots .cur {
background-color: red;
}
#swiper .left, #swiper .right {
position: absolute;
width: 42px;
height: 70px;
background: url(./images/icon-slides.png) no-repeat -84px 0;
top: 165px;
cursor: pointer;
}
#swiper .right {
right: 0px;
background-position: -126px 0;
}
#swiper .left:hover {
background-position: 0 0;
}
#swiper .right:hover {
background-position: -42px 0;
}
</style>
</head>
<body>
<div id="swiper">
<div class="images">
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/a532e33470d046b3f044d5ea49fc5e9e.png?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/918820682e4a490221cfd92b24c14b86.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/67c7222fbcf98db942718eaf29f32297.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/01a5d7313fe344bddb9e4081df4dc998.jpg?w=2452&h=920"/>
</div>
<div class="dots">
<span class="cur"></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
<script>
let swiper = document.querySelector("#swiper");
let img = document.querySelector("#swiper .images");
let last = img.querySelector("img").cloneNode();
img.appendChild(last);
let lastTime = null;
let counterTime = 1000;
document.querySelector("#swiper .right").addEventListener("click",function (event){
let count = 0;
let now = new Date().getTime();
if(lastTime != null){
if(now - lastTime < counterTime )return;
}
lastTime = now;
let playing = setInterval(function (){
count ++ ;
img.scrollLeft = img.scrollLeft + swiper.clientWidth / 10 ;
if (count >= 10) {
clearInterval(playing);
if (img.scrollLeft >= (img.querySelectorAll("img").length - 1) * swiper.clientWidth) {
img.scrollLeft = 0 ;
}
let index = img.scrollLeft / swiper.clientWidth ;
document.querySelector("#swiper .dots .cur").classList.remove("cur");
document.querySelector(`#swiper .dots span:nth-of-type(${index + 1})`).classList.add("cur")
}
},100)
})
</script>
</body>
</html>
js-滚动轮播图-下翻
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#swiper {
position: relative;
width: 960px;
height:400px;
margin:100px auto 0 ;
font-size: 0;
}
#swiper .images {
position: absolute;
width: 100%;
height:100%;
white-space: nowrap;
overflow: scroll hidden;
}
#swiper img {
width: 100%;
height: 100%;
}
#swiper .dots {
position: absolute;
height: 40px;
bottom: 0px;
background-color: rgba(0, 0, 0, 0.6);
width: 100%;
font-size: 0;
text-align: center;
}
#swiper .dots span {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
margin: 15px 5px;
cursor: pointer;
}
#swiper .dots .cur {
background-color: red;
}
#swiper .left, #swiper .right {
position: absolute;
width: 42px;
height: 70px;
background: url(./images/icon-slides.png) no-repeat -84px 0;
top: 165px;
cursor: pointer;
}
#swiper .right {
right: 0px;
background-position: -126px 0;
}
#swiper .left:hover {
background-position: 0 0;
}
#swiper .right:hover {
background-position: -42px 0;
}
</style>
</head>
<body>
<div id="swiper">
<div class="images">
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/a532e33470d046b3f044d5ea49fc5e9e.png?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/918820682e4a490221cfd92b24c14b86.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/67c7222fbcf98db942718eaf29f32297.jpg?thumb=1&w=1226&h=460&f=webp&q=90"/>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/01a5d7313fe344bddb9e4081df4dc998.jpg?w=2452&h=920"/>
</div>
<div class="dots">
<span class="cur"></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
<script>
let swiper = document.querySelector("#swiper");
let img = document.querySelector("#swiper .images");
let last = img.querySelector("img").cloneNode();
img.appendChild(last);
document.querySelector("#swiper .right").addEventListener("click", function(event){
let count = 0 ;
let playing = setInterval(function(){
count ++ ;
img.scrollLeft = img.scrollLeft + swiper.clientWidth / 10 ;
if (count >= 10) {
clearInterval(playing);
if (img.scrollLeft >= (img.querySelectorAll("img").length - 1) * swiper.clientWidth) {
img.scrollLeft = 0 ;
}
let index = img.scrollLeft / swiper.clientWidth ;
document.querySelector("#swiper .dots .cur").classList.remove("cur");
document.querySelector(`#swiper .dots span:nth-of-type(${index + 1})`).classList.add("cur")
}
}, 100)
})
</script>
</body>
</html>
js-事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#app {
width: 300px;
height: 300px;
background-color: red;
overflow: hidden;
}
#inner {
width: 150px;
height: 150px;
margin: 75px;
background-color: blue;
}
</style>
</head>
<body>
<div id="app">
<div id="inner"></div>
</div>
<script>
document.querySelector("#app").addEventListener("click", function(event) {
console.log("app... 被点击")
})
document.querySelector("#inner").addEventListener("click", function(event) {
console.log("inner... 被点击")
event.stopPropagation();
})
</script>
</body>
</html>
js-事件捕获
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#app {
width: 300px;
height: 300px;
background-color: red;
overflow: hidden;
}
#inner {
width: 150px;
height: 150px;
margin: 75px;
background-color: blue;
}
</style>
</head>
<body>
<div id="app">
<div id="inner"></div>
</div>
<script>
document.querySelector("#app").addEventListener("click", function (event) {
console.log("app... 被点击")
event.stopPropagation()
}, true)
document.querySelector("#inner").addEventListener("click", function (event) {
console.log("inner... 被点击")
}, true)
</script>
</body>
</html>
js-load事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function test() {
alert(alert)
}
</script>
</head>
<body onload="test()">
<p>hello</p>
<img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/01a5d7313fe344bddb9e4081df4dc998.jpg?w=2452&h=920" />
</body>
</html>
js-放大镜
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#zoom {
position: relative;
width: 500px;
height:500px;
margin: 100px auto 0 ;
}
</style>
<script src="js/commons.js"></script>
</head>
<body>
<div id="zoom">
<img src="./images/放大镜.png" width="400px"/>
</div>
<script>
let img = document.querySelector("#zoom img")
img.zoom();
</script>
</body>
</html>
js-本地存储-cookie
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/commons.js"></script>
<script>
console.log(getCookie("xxx"));
removeCookie("xxx")
console.log(localStorage.getItem("xxxx"));
console.log(sessionStorage.getItem("xxxxx"));
</script>
</head>
<body>
</body>
</html>
.js文件
function getClientPos(target) {
let left = 0, tops = 0;
while (target.offsetParent) {
left += target.offsetLeft + target.offsetParent.clientLeft;
tops += target.offsetTop + target.offsetParent.clientTop;
target = target.offsetParent;
}
return {left, tops}
}
HTMLImageElement.prototype.zoom = function () {
let minBorder = this.clientWidth > this.clientHeight ? this.clientHeight : this.clientWidth;
let wh = Math.floor(minBorder / 2);
let zoomDiv = document.createElement("div");
zoomDiv.style.position = "absolute" ;
zoomDiv.style.left = this.offsetLeft + this.offsetWidth + "px" ;
zoomDiv.style.top = this.offsetTop + "px" ;
this.parentNode.appendChild(zoomDiv);
let that = this;
this.parentNode.addEventListener("mousemove", function (event) {
let {left, tops} = getClientPos(that);
let {left: pLeft, tops: pTop} = getClientPos(this);
if (event.pageX - left >= 0 && event.pageX - left <= that.clientWidth &&
event.pageY - tops >= 0 && event.pageY - tops <= that.clientHeight) {
zoomDiv.style.display = "block" ;
if (this.model == null) {
let model = document.createElement("div");
model.style.width = wh + "px";
model.style.height = wh + "px";
model.style.background = "rgba(200, 200, 200, 0.4)";
model.style.position = "absolute";
model.style.cursor = "move";
this.model = model;
this.appendChild(model);
model.addEventListener("mouseout", e=>{
model.remove();
this.model = null ;
zoomDiv.style.display = "none";
})
}
let offsetLeft = event.pageX - this.model.offsetWidth / 2 - pLeft;
let offsetTop = event.pageY - this.model.offsetHeight / 2 - pTop;
this.model.style.left = offsetLeft + "px";
this.model.style.top = offsetTop + "px";
if (this.model.offsetLeft < that.offsetLeft) {
this.model.style.left = that.offsetLeft + "px";
}
if (that.offsetLeft + that.offsetWidth - that.clientLeft - this.model.clientWidth < this.model.offsetLeft) {
this.model.style.left = that.offsetLeft + that.offsetWidth - that.clientLeft - this.model.clientWidth + "px"
}
if (this.model.offsetTop < that.offsetTop) {
this.model.style.top = that.offsetTop + "px";
}
if (that.offsetTop + that.offsetHeight - that.clientTop - this.model.clientHeight < this.model.offsetTop) {
this.model.style.top = that.offsetTop + that.offsetHeight - that.clientTop - this.model.clientHeight + "px"
}
let rx = this.model.offsetLeft - that.offsetLeft ;
let ry = this.model.offsetTop - that.offsetTop ;
let image = new Image() ;
image.src = that.src ;
image.onload = function() {
let w = this.width ;
let h = this.height ;
let x = rx / that.clientWidth * w ;
let y = ry / that.clientHeight * h ;
let zw = wh / that.clientWidth * w ;
let zh = wh / that.clientHeight * h ;
zoomDiv.style.width = zw + "px" ;
zoomDiv.style.height = zh + "px";
zoomDiv.style.backgroundImage = `url(${this.src})` ;
zoomDiv.style.backgroundRepeat = "no-repeat";
zoomDiv.style.backgroundPosition = `-${x}px -${y}px` ;
}
}
})
}
commons.js:
Date.prototype.format = function (pattern = "yyyy-MM-dd HH:mm:ss") {
let year = this.getFullYear() + "";
let month = this.getMonth() + 1 < 10 ? "0" + (this.getMonth() + 1) : this.getMonth() + 1;
let date = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
let hour = this.getHours() < 10 ? "0" + this.getHours() : this.getHours();
let min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes();
let sec = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds();
let mill = (this.getMilliseconds() + "").padStart(3, "0");
let weekArray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
let week = weekArray[this.getDay()];
return pattern.replace("yyyy", year)
.replace("MM", month)
.replace("dd", date)
.replace("HH", hour)
.replace("hh", hour > 12 ? hour - 12 : hour)
.replace("mm", min)
.replace("ss", sec)
.replace("SSS", mill)
.replace("E", week)
}
function getClientPos(target) {
let left = 0, tops = 0;
while (target.offsetParent) {
left += target.offsetLeft + target.offsetParent.clientLeft;
tops += target.offsetTop + target.offsetParent.clientTop;
target = target.offsetParent;
}
return {left, tops}
}
function randomRgb() {
let r = Math.floor(Math.random() * 256)
let g = Math.floor(Math.random() * 256)
let b = Math.floor(Math.random() * 256)
return `rgb(${r}, ${g}, ${b})`
}
HTMLImageElement.prototype.zoom = function () {
let minBorder = this.clientWidth > this.clientHeight ? this.clientHeight : this.clientWidth;
let wh = Math.floor(minBorder / 2);
let zoomDiv = document.createElement("div");
zoomDiv.style.position = "absolute" ;
zoomDiv.style.left = this.offsetLeft + this.offsetWidth + "px" ;
zoomDiv.style.top = this.offsetTop + "px" ;
this.parentNode.appendChild(zoomDiv);
let that = this;
this.parentNode.addEventListener("mousemove", function (event) {
let {left, tops} = getClientPos(that);
let {left: pLeft, tops: pTop} = getClientPos(this);
if (event.pageX - left >= 0 && event.pageX - left <= that.clientWidth &&
event.pageY - tops >= 0 && event.pageY - tops <= that.clientHeight) {
zoomDiv.style.display = "block" ;
if (this.model == null) {
let model = document.createElement("div");
model.style.width = wh + "px";
model.style.height = wh + "px";
model.style.background = "rgba(255, 255, 0, 0.6)";
model.style.position = "absolute";
model.style.cursor = "move";
this.model = model;
this.appendChild(model);
model.addEventListener("mouseout", e=>{
model.remove();
this.model = null ;
zoomDiv.style.display = "none";
})
}
let offsetLeft = event.pageX - this.model.offsetWidth / 2 - pLeft;
let offsetTop = event.pageY - this.model.offsetHeight / 2 - pTop;
this.model.style.left = offsetLeft + "px";
this.model.style.top = offsetTop + "px";
if (this.model.offsetLeft < that.offsetLeft) {
this.model.style.left = that.offsetLeft + "px";
}
if (that.offsetLeft + that.offsetWidth - that.clientLeft - this.model.clientWidth < this.model.offsetLeft) {
this.model.style.left = that.offsetLeft + that.offsetWidth - that.clientLeft - this.model.clientWidth + "px"
}
if (this.model.offsetTop < that.offsetTop) {
this.model.style.top = that.offsetTop + "px";
}
if (that.offsetTop + that.offsetHeight - that.clientTop - this.model.clientHeight < this.model.offsetTop) {
this.model.style.top = that.offsetTop + that.offsetHeight - that.clientTop - this.model.clientHeight + "px"
}
let rx = this.model.offsetLeft - that.offsetLeft ;
let ry = this.model.offsetTop - that.offsetTop ;
let image = new Image() ;
image.src = that.src ;
image.onload = function() {
let w = this.width ;
let h = this.height ;
let x = rx / that.clientWidth * w ;
let y = ry / that.clientHeight * h ;
let zw = wh / that.clientWidth * w ;
let zh = wh / that.clientHeight * h ;
zoomDiv.style.width = zw + "px" ;
zoomDiv.style.height = zh + "px";
zoomDiv.style.backgroundImage = `url(${this.src})` ;
zoomDiv.style.backgroundRepeat = "no-repeat";
zoomDiv.style.backgroundPosition = `-${x}px -${y}px` ;
}
}
})
}
|