<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#div_1 {
width: 100px;
height: 100px;
transition: left 2s;
position: absolute;
top: 50px;
background-color: red;
}
.noAnimate {
transition: none;
}
</style>
<script>
function domRedraw_1(dom) {
// 藉著寬度改變
var css = getComputedStyle(dom, null);
var oldStyle = dom.style.cssText;
/* ------------------------ */
/**
* 改變寬度
*
* 寬度+1
*
* 再改回來
*/
var width = css.width.replace(/\D/g, '');
width = Number(width);
width++;
dom.style.width = width + 'px';
/* ------------------------ */
// 回復
dom.style.cssText = oldStyle;
};
function domRedraw_2(dom) {
// 藉著(offsetHeight)
dom.offsetHeight;
};
</script>
<script>
function test_1() {
var div = document.querySelector('#div_1');
var style = div.style;
div.classList.add('noAnimate');
style.cssText = 'left:300px;';
// do this to force a redraw instead of setTimeout() !
// div[0].offsetHeight;
domRedraw_1(div);
// domRedraw_2(div);
div.classList.remove('noAnimate');
style.cssText = 'left:0;';
/*
setTimeout(function() {
div.removeClass('noAnimate').css('left', 0);
}, 10);
*/
}
</script>
</head>
<body>
<button onclick="test_1();">Click!</button>
<div id="div_1">
target
</div>
</body>
</html>
沒有留言:
張貼留言