我有一个页面使用spin.js(http://fgnass.github.io/spin.js/#!)纯Javascript微调器在单击链接(指向慢速页面)时显示微调器。我使用链接上的点击触发它。
问题出在 Chrome (v 30.0.1599.69 m) 中,如果我中键单击链接以在新选项卡中打开慢速页面,那么当我返回原始页面时,微调器正在运行并永远运行。它不会发生在IE或Firefox中。
(显然,如果我左键单击链接,旋转器将在加载新页面时死亡。
我的链接如下所示: <a href="other-sources.php?id='.$id.'" onClick="return spinner(40);">Other Sources</a>
启动微调器的脚本如下所示:
<script type="text/javascript">
// Display spinner while waiting for Other Sources to display.
// Parameter is vertical position of spinner, since we have 2 Show Source links
function spinner(posn) {
var opts = {
lines: 15, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 15, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
color: '#000', // #rgb or #rrggbb
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: posn, // Top position relative to parent in px
left: 850 // Left position relative to parent in px
};
var target = document.getElementById('main');
var spinner = new Spinner(opts).spin(target);
return true;
}
</script>
为了完成图片,我在"头部"部分中有这个:
<script src="spin.min.js"></script>
我可以在 javascript 中放入一些东西来让它忽略中间按钮点击吗?我在其他地方读到,检测按钮相当古怪。
嗯。我看到有一个关于它的错误报告可以追溯到 2008 年:https://code.google.com/p/chromium/issues/detail?id=1687
好的,我按如下方式解决了问题:
-
将链接从单击更改为鼠标向上
-
在 javascript 的开头添加了以下代码:
if (event.which == null) /* IE case */ var button= (event.button < 2) ? "LEFT" : ((event.button == 4) ? "MIDDLE" : "RIGHT"); else /* All others */ var button= (event.which < 2) ? "LEFT" : ((event.which == 2) ? "MIDDLE" : "RIGHT"); if (button != "LEFT") return true;