點擊跳轉html
<style>
.jigsaw {
margin-top: 15px;
}
.jigsaw .ul{
display: flex;
flex-wrap: wrap; // 換行
}
.ul .child {
width: 33.3%;
color: rgba(255, 255, 255, 0.5);
background-repeat: no-repeat;
}
</style>
<section class='jigsaw'>
<ul class="ul">
<li class="child" data-x='1' data-y='1' style="order:1">1</li>
<li class="child" data-x='2' data-y='1' style="order:2">2</li>
<li class="child" data-x='3' data-y='1' style="order:3">3</li>
<li class="child" data-x='1' data-y='2' style="order:4">4</li>
<li class="child" data-x='2' data-y='2' style="order:5">5</li>
<li class="child" data-x='3' data-y='2' style="order:6">6</li>
<li class="child" data-x='1' data-y='3' style="order:7">7</li>
<li class="child" data-x='2' data-y='3' style="order:8">8</li>
<li class="child" data-x='3' data-y='3' style="order:9" id='empty'>9</li>
</ul>
</section>
複製代碼
// 設置格子的高度、背景圖片的尺寸
setChildStyle() {
this.childWidth = window.getComputedStyle(this.oChild[0], false).width; // 獲取格子寬度
console.log(this.childWidth);
for (let i = 0; i < this.oChild.length; i++) {
this.oChild[i].style.height = `${this.childWidth}`;
}
}
複製代碼
setChildStyle() {
this.childWidth = window.getComputedStyle(this.oChild[0], false).width;
console.log(this.childWidth);
for (let i = 0; i < this.oChild.length; i++) {
this.oChild[i].style.height = `${this.childWidth}`;
this.oChild[i].style.backgroundSize = `${this.ulWidth} auto`;
this.setBgpositon(this.oChild[i]);
}
}
複製代碼
// 設置背景圖在格子的位置
setBgpositon(chiObj) {
let x = chiObj.getAttribute('data-x') - 1;
let y = chiObj.getAttribute('data-y') - 1;
chiObj.style.backgroundPosition = `${-x*parseInt(this.childWidth)}px ${-y*parseInt(this.childWidth)}px`;
}
複製代碼
以下圖視: git
// 設置格子的背景圖片
setBgImg(imgUrl) {
for (let i = 0; i < this.oChild.length - 1; i++) {
this.oChild[i].style.backgroundImage = `url(${imgUrl})`;
}
}
複製代碼
childEvent() {
let that = this;
let oEmptyChild = document.getElementById('empty'); // 獲取空白的格子對象
this.oUl[0].addEventListener('click', function(ev){
let target = ev.target;
let targetX, targetY, targetOrder;
let iEmptyX, iEmptyY, iEmptyOrder;
if (target.className != 'child' ) return false;
iEmptyX = oEmptyChild.getAttribute('data-x');
iEmptyY = oEmptyChild.getAttribute('data-y');
iEmptyOrder = window.getComputedStyle(oEmptyChild, false).order;
targetX = target.getAttribute('data-x');
targetY = target.getAttribute('data-y');
targetOrder = window.getComputedStyle(target, false).order;
if (Math.abs(targetX - iEmptyX) + Math.abs(targetY - iEmptyY) == 1) {
// data-x data-y order 值互換
[iEmptyX, targetX] = [targetX, iEmptyX];
[iEmptyY, targetY] = [targetY, iEmptyY];
[iEmptyOrder, targetOrder] = [targetOrder, iEmptyOrder];
oEmptyChild.setAttribute('data-x', iEmptyX);
oEmptyChild.setAttribute('data-y', iEmptyY);
oEmptyChild.style.order = iEmptyOrder;
target.setAttribute('data-x', targetX);
target.setAttribute('data-y', targetY);
target.style.order = targetOrder;
}
}, false);
}
複製代碼
imgEvent() {
let that = this;
this.oFile.addEventListener('change', function(){
let imgUrl = window.URL.createObjectURL(this.files[0]); // 該方法將files轉換成img可訪問的本地路徑
that.oImg.setAttribute('src', imgUrl);
that.oImg.onload = function() {
that.setBgImg(imgUrl); // 從新設置格子背景
}
}, false);
}
複製代碼
源碼地址 點擊訪問github
(完)bash