10

jQueryで開いたウィンドウを画面の右上隅に配置する方法はありますか?

これは私が今持っているコードです:

$(document).ready(function() {
    $("#dirs a").click(function() {
        // opens in new window
        window.open(this.href, "customWindow", "width=960, height=1040");
        return false;
    });
});

そして、Vista以降でWindows Aeroスナップで「スナップ」した場合と同様に、画面の右上隅に開くようにします。これを実現する方法はありますか?

ちなみに、これは私だけが使用する単純なページであり、1920x1080 モニターの Chrome でのみ使用するため、さまざまなブラウザーや画面サイズに合わせて調整するための凝ったものは必要ありません。

4

5 に答える 5

21

右上にしたいならこれはいらないのでは?

window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=960");
于 2012-05-23T21:40:05.573 に答える
7

JavaScript window.open は多くのパラメーターを受け入れます。あなたの特定のケースでは、上と左で十分です。

実際のフィドルの例を見てください!

構文

window.open([URL], [Window Name], [Feature List], [Replace]);

機能一覧

ここに画像の説明を入力

ニーズに合わせた実例

<script type="text/javascript">
<!--
function popup(url) 
{
 var width  = 960;
 var height = 1040;
 var left   = screen.width - 960;
 var top    = 0;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'customWindow', params);
 if (window.focus) {newwin.focus()}
 return false;
}
// -->
</script>

<a href="javascript: void(0)" 
   onclick="popup('popup.html')">Top Right popup window</a>

注: これにより、画面の幅が計算され、left適切に設定されます。

高さが大きいウィンドウを使用していることを考慮してください。通常、画面は高さよりも大きくなっています...

于 2012-05-23T21:47:32.473 に答える
1
$("#dirs a").click(function(e) {
    e.preventDefault();
    var popupWidth = 960;
    var leftPos = screen.width - popupWidth;

    window.open(this.href, "customWindow", "width=" + popupWidth + ", height=1040, top=0, left=" + leftPos);
});

ここにがあります。

于 2012-05-23T21:37:19.700 に答える
1
window.open(this.href, "customWindow", "width=960, height=1040, top=0, left=0");

その他のウィンドウ プロパティ:

Property         Default value   Description
width            auto            specifies width of the new window in pixels
height           auto            height of the window in pixels
top              auto            specifies window position
left             auto            specifies window position
directories      no              should the directories bar be shown? (Links bar)
location         no              specifies the presence of the location bar
resizable        no              specifies whether the window can be resized.
menubar          no              specifies the presence of the menu bar
toolbar          no              specifies the presence of the toolbar
scrollbars       no              specifies the presence of the scrollbars
status           no              specifies the presence of the statusbar
于 2012-05-23T21:38:16.933 に答える
1

これは正しいものです.最初に画面の幅を計算する必要があります。

var leftPos = screen.width - 960;
window.open(this.href, "customWindow", "width=960, height=1040, top=40, left="+leftPos+");
于 2012-05-23T21:42:03.863 に答える