目前想做一個拖拽排序且?guī)赢嫷墓δ?,?jīng)過考慮和參考后,發(fā)現(xiàn)大部分解決方法都是將元素改為絕對定位,之后通過 transition 或別的方式插值處理 left 和 top,于是測試后這種方式。
但編碼中發(fā)現(xiàn)一個問題,使用 js 或 jQuery 遍歷并修改元素css時,會出現(xiàn)所有元素被賦予第一個元素的CSS樣式。
當(dāng)不修改CSS時,打印的css變量均正常,但如果修改,則后續(xù)所有變量值都會變成第一次的值。
不知該問題為什么引起又如何解決?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
box-sizing: border-box;
}
.container {
display: flex;
width: 100%;
flex-wrap: wrap;
position: relative;
}
.box {
flex: 0 0 25%;
border: 1px solid #999;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="box" draggable="true">1</div>
<div class="box" draggable="true">2</div>
<div class="box" draggable="true">3</div>
<div class="box" draggable="true">4</div>
<div class="box" draggable="true">5</div>
<div class="box" draggable="true">6</div>
<div class="box" draggable="true">7</div>
<div class="box" draggable="true">8</div>
<div class="box" draggable="true">9</div>
<div class="box" draggable="true">10</div>
<div class="box" draggable="true">11</div>
<div class="box" draggable="true">12</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('.box').each(function () {
var css = {
'position': 'absolute',
'left': $(this)[0].offsetLeft,
'top': $(this)[0].offsetTop,
'width': $(this)[0].offsetWidth,
'height': $(this)[0].offsetHeight
};
console.log(css);
$(this).removeClass('box');
// 如果執(zhí)行下面一行,則所有的 css 內(nèi)容都會變成第一個元素的值
// position:absolute;left: 0px; top: 10px;width: 226px;height: 23px
$(this).css(css);
});
});
</script>
</body>
</html>
幾天后我回顧了此問題,將自己最終的理解重新整理。
該問題的重點,在于jQuery
的css()
方法或javascript
修改style
都會立即觸發(fā)重排。
修改css
并移除box
后,瀏覽器已經(jīng)立即將第二項移動到了第一項的位置。
每次修改css
后都會觸發(fā)重排,所以解決方案就是在取到所有css
數(shù)據(jù)后(循環(huán)結(jié)束后),再修改樣式。
也就是:
$('.box').each(function () {
var css = {
'position': 'absolute',
'left': $(this)[0].offsetLeft,
'top': $(this)[0].offsetTop,
'width': $(this)[0].offsetWidth,
'height': $(this)[0].offsetHeight
};
var that = $(this);
setTimeout(function () {
that.css(css);
that.removeClass('box');
}, 0);
});
你有10個盒子累起來了,你決定抽調(diào)最后一個把它粘在墻上,然后其余9個都掉下來了, 所以每次你獲取的最后一個盒子位置總是一樣的, 因為他們總是在最底部.
解決方案就是不要立即抽調(diào)最后一個盒子,把尺寸都測量完后在執(zhí)行抽出的操作.
function queueUpdate($el, css) {
setTimeout(function () {
$el.css(css)
}, 0)
}
$(document).ready(function () {
$('.box').each(function () {
var css = {
'position': 'absolute',
'left': $(this)[0].offsetLeft,
'top': $(this)[0].offsetTop,
'width': $(this)[0].offsetWidth,
'height': $(this)[0].offsetHeight
};
$(this).removeClass('box');
// 如果執(zhí)行下面一行,則所有的 css 內(nèi)容都會變成第一個元素的值
// position:absolute;left: 0px; top: 10px;width: 226px;height: 23px
queueUpdate($(this), css)
});
});
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。