-1

モバイル Web レイアウトなどのプログラミングはあまり得意ではありません。

私はこれを書いた:

HTML:

<div id="title">...</div>

JavaScript

$("#title").css("margin-left", "7%");
$("#title").css("margin-right", "7%");

var i = screen.width * 0.018 + "px";          // It would be 24.588 at resolution of 1366
$("#title").css("margin-top", i);                // And i wanted to use 24.588px here 
$("#title").css("margin-bottom", i);         // and here.

また、Chrome は左右に div マージンを表示しますが、上下の MARGINS をレンダリングしません...

4

3 に答える 3

4

次のように、すべてのmargin_*プロパティをアンダースコアではなく-ダッシュで区切る必要があります。

$("#title").css("margin-left", "7%");
$("#title").css("margin-right", "7%");

var i = screen.width * 0.018 + "px";
$("#title").css("margin-top", i);
$("#title").css("margin-bottom", i);

jQuery の.css()メソッドは、この形式で使用される場合、CSS プロパティ名を最初の引数として取り、設定する値を 2 番目の引数として取ります。

別のより良い方法は、オブジェクトを に渡すことです.css()

var margin = screen.width * 0.018 + "px";

$('#title').css({
    marginLeft:   "7%",
    marginRight:  "7%",
    marginTop:    margin,
    marginBottom: margin
});

ここでは、属性のキャメル ケース バージョンを使用する必要があることに注意してください。

また、上下の余白を JavaScript で計算する代わりに、パーセンテージを使用してみませんか?

于 2013-10-26T17:38:53.347 に答える
1

アンダースコアやダッシュは必要ありません。次のように大文字を使用する必要があります。

$("#title").css("marginLeft", "7%");
于 2013-10-26T17:41:15.937 に答える
0

This might be more simple.

function marge() {
    var sw = $(window).width() * 0.018 + "px";//get margin
    $("#title").css({margin: sw+' 7%'});//set margin tb lr
}
marge();//run initially
$(window).resize(function() { marge(); });//to adjust on resize of the window

lines 2 & 3 solve the issue, the rest allows it to run the function again on window resize if needed.

made a fiddle: http://jsfiddle.net/filever10/z2HQL/

于 2013-10-26T18:20:35.750 に答える