Javascript 警报位置不在 chrome 的中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19151560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript alert position not in center in chrome
提问by AtanuCSE
When I'm using an javascript alert it's going on top of the browser, not in centerposition in current version of chrome. How can I control it, and make it center with javascript. In firefox and IE,it's working just fine. My current chrome version 30.0.1599.66
当我使用 javascript 警报时,它会出现在浏览器的顶部,而不是当前版本的 chrome中的中心位置。我如何控制它,并使其以javascript为中心。在 Firefox 和 IE 中,它工作得很好。我当前的 chrome 版本 30.0.1599.66
I want to position the alert box in exact center of the browser for all type of browsers and for all versions. Please help........
对于所有类型的浏览器和所有版本,我想将警报框放置在浏览器的正中央。请帮忙........
Any other simple idea on providing alert so that it could be centered, would also be appreciable...
关于提供警报以便它可以居中的任何其他简单想法也将是可观的......
采纳答案by Vikram Tiwari
Being dependent upon the browsers for the alerts is not a good choice for any. They change rapidly and are not the same for different browsers.
依赖浏览器来获取警报对任何人来说都不是一个好的选择。它们变化很快,对于不同的浏览器也不相同。
What i have found useful is to use Alertify JSfor all the alert needs. You can customize it for your needs and it looks fabulous anyway.
我发现有用的是使用Alertify JS来满足所有警报需求。您可以根据自己的需要自定义它,无论如何它看起来都很棒。
回答by Manu
Since it is a default box, u cannot position it, though u can create your own and position it accordingly.. try this http://jqueryui.com/dialog/
由于它是默认框,因此您无法定位它,但您可以创建自己的框并相应地定位它.. 试试这个 http://jqueryui.com/dialog/
回答by Carlos Sousa
You want alert() dialog position in center. Try this simple script.js
您希望 alert() 对话框位置居中。试试这个简单的 script.js
// alertMX - improve alert()
$("<style type='text/css'>#boxMX{display:none;background: #333;padding: 10px;border: 2px solid #ddd;float: left;font-size: 1.2em;position: fixed;top: 50%; left: 50%;z-index: 99999;box-shadow: 0px 0px 20px #999; -moz-box-shadow: 0px 0px 20px #999; -webkit-box-shadow: 0px 0px 20px #999; border-radius:6px 6px 6px 6px; -moz-border-radius: 6px; -webkit-border-radius: 6px; font:13px Arial, Helvetica, sans-serif; padding:6px 6px 4px;width:300px; color: white;}</style>").appendTo("head");
function alertMX(t){
$( "body" ).append( $( "<div id='boxMX'><p class='msgMX'></p><p>CLOSE</p></div>" ) );
$('.msgMX').text(t); var popMargTop = ($('#boxMX').height() + 24) / 2, popMargLeft = ($('#boxMX').width() + 24) / 2;
$('#boxMX').css({ 'margin-top' : -popMargTop,'margin-left' : -popMargLeft}).fadeIn(600);
$("#boxMX").click(function() { $(this).remove(); }); };
Include Jquery and use javascript:
包括 Jquery 并使用 javascript:
alertMX('Hello!');
回答by Tomasz Kap?oński
You cannot control the way browser display alert. Instead, you should write your own function to display div with your message. It could be something like that:
您无法控制浏览器显示警报的方式。相反,您应该编写自己的函数来显示带有消息的 div。它可能是这样的:
function customAlert(msg) {
var alertDiv = "<div style='position: fixed; top: 20px; left: 20px;'>"+msg+"</div>";
document.getElementsByTagName('body')[0].appendChild(alertDiv);
}
Ofcourse you should do some calculation there to properly position the div where you want. Also it would be much easier if you use jQuery or some other js framework...
当然,您应该在那里进行一些计算,以将 div 正确放置在您想要的位置。如果您使用 jQuery 或其他一些 js 框架,它也会容易得多......
[Edit] Try something like that if you want to force popup from JS. Popup example
[编辑] 如果您想从 JS 强制弹出,请尝试类似的操作。 弹出示例