<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
border: 1px solid #ccc;
background-image: url(./1.jpg);
width: 500px;
height: 500px;
background-position: 0px 0px;
background-repeat: no-repeat;
background-size: 1920px 1080px;
}
</style>
</head>
<body>
<div style="position:relative">
<img id="clothes" class="clothes" src="./1.jpg" alt="" width="500">
</div>
<div class="box" id="box"></div>
<script>
function getImgNatural(img,callback){
var nWidth,nHeight
if(img.naturalWidth){
nWidth=img.naturalWidth
nHeight=img.naturalHeight
callback(nWidth,nHeight)
}else{
var image=new Image()
image.src=img.src
image.onload=function(){
callback(image.width,image.height)
}
}
}
function test(img,callback){
var image=new Image()
image.src=img.src
image.onload=function(){
callback(image.width,image.height)
image=null
}
}
function test2(img,callback){
return new Promise((resolve,reject)=>{
var image=new Image()
image.src=img.src
image.onload=function(){
resolve([image.width,image.height])
image=null
}
})
}
test2(img).then((res)=>{
console.log(res);
})
let img = document.getElementById('clothes')
img.onmousemove = (e) => {
const { top, left, bottom, right } = img.getBoundingClientRect()
let x = e.clientX, y = e.clientY
console.log(x, y);
let width = img.clientWidth
let height = img.clientHeight
let ab_x = (x - left) / width
let ab_y = (y - top) / height
console.log(ab_x, ab_y);
test(img,handle)
function handle(maxwidth,maxheight){
console.log(maxwidth,maxheight);
let res_x = ab_x*maxwidth
let res_y = ab_y*maxheight
if (res_x > maxwidth - 500) {
res_x = maxwidth - 500
} else if (res_x < 0) {
res_x = 0
}
if (res_y > maxheight - 500) {
res_y = maxheight - 500
} else if (res_y < 0) {
res_y = 0
}
console.log(`${res_x}px ${res_y}px`);
document.getElementById('box').style.backgroundPosition = `-${res_x}px -${res_y}px`
}
}
</script>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探