This html文件显示3面板布局。 "左"上的css过渡当我更换面板时,属性给我一个很好的滑动效果。这一切都有效但是,当我更换面板时,我想将焦点放在新面板的输入上。任何人都可以告诉我为什么对focus()的调用打破了css转换,并让我在两个面板之间中途停留?它只发生在前进。如果我注释掉转换,它就可以工作,如果我将调用延迟调用focus()并且超时。
问题出现在Chrome,IE和Firefox中。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Sliding Bug</title>
<style>
#wrapper {
width: 100%;
height: auto;
overflow: hidden;
}
#panelContainer {
position: relative;
transition: left 1000ms ease;
}
.panel {
float: left;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="panelContainer">
<div id="panel0" class="panel" style="background-color: #888;">
panel zero
<input id="btn0" class="focussable" type="button" onclick="slide(1);" value="forward" />
</div>
<div id="panel1" class="panel" style="background-color: #AAA;">
panel 1
<input id="btn1" class="focussable" type="button" onclick="slide(2);" value="forward" />
<input type="button" onclick="slide(0);" value="back" />
</div>
<div id="panel2" class="panel" style="background-color: #CCC;">
panel 2
<input id="btn2" class="focussable" type="button" onclick="slide(1);" value="back" />
</div>
</div>
</div>
<script>
var panelContainer;
var panels = new Array();
var numPanels;
var currentPanel = 0;
window.addEventListener('load', init, false);
function init() {
setTimeout(function () { window.scrollTo(0, 1); }, 10); //hiding the browser address bar in iPhone/Android
panelContainer = document.getElementById("panelContainer");
panels = document.getElementsByClassName("panel");
numPanels = panels.length;
sizeResize();
}
function sizeResize() {
panelContainer.style.width = (numPanels * window.innerWidth) + "px";
panelContainer.style.height = window.innerHeight + "px";
for (var i = 0; i < numPanels; i++) {
panels[i].style.width = window.innerWidth + "px";
panels[i].style.height = window.innerHeight + "px";
}
slide();
}
window.onresize = sizeResize;
function slide(panel) {
if (panel != undefined)
currentPanel = panel;
panelContainer.style.left = -(window.innerWidth * currentPanel) + "px";
if (panel >= 0) {
panels[panel].getElementsByTagName("input")[0].focus();//shows problem
//window.setTimeout(function () { panels[panel].getElementsByTagName("input")[0].focus() }, 100);//no problem
}
}
</script>
</body>
</html>
答案 0 :(得分:4)
正如@ user1585345正确声明的那样,浏览器会立即滚动到焦点元素,以便将其置于当前视图中,而不是px!
这里会发生以下情况:您更改元素的相对位置,但是立即更改焦点,因为当事件触发时焦点元素不可见,内容也滚动(结束动画突然)。
只有在动画完成时才应显式激活焦点,以便浏览器识别出不需要滚动。