English 中文(简体)
Jquery CSS How to Use Margin: 零
原标题:Jquery CSS How To Use Margin: 0 auto
  • 时间:2012-04-25 14:35:44
  •  标签:
  • jquery
  • css

I m New to Jquery. 我想知道我们如何使用差值:0辆汽车;CSS在jquery描述中的代码。 谁能帮助我? 该法典:

<style>
#fixed-bar {
    padding: 0;
    background-color: black;
    z-index: 100;
}
</style>
<script src= http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js ></script>

<script>
$(function () {
  $("#fixed-bar")
    .css({
        "position": "fixed",
        "width": "960px",
        "margin": "0 auto",
        "top": "0px",

})
    .hide();
  $(window).scroll(function () {
    if ($(this).scrollTop() > 400) {
      $( #fixed-bar ).fadeIn(200);
    } else {
      $( #fixed-bar ).fadeOut(200);
    }
  });
});
</script>
<div id= fixed-bar >
  Hello
</div>

实际上,我想把酒吧集中起来。 我如何能够这样做?

最佳回答

You are setting everything correct. But you cannot center an element with margin: auto that has position: fixed:

中心职位:固定要素

你也可以这样做。

使用 j Query to central a DIV on the Screen

问题回答

You can t use shorthand CSS with jQuery, you would have to set each margin separately.

css({
    "marginTop": "0",
    "marginRight": "auto",
    "marginBottom": "0",
    "marginLeft": "auto"
})

You can try this :

<>光>:

<div id="fixed-bar">
    <p>Test</p>
</div>

<>CSS:

body {
    position : relative;
}

#fixed-bar {
    padding: 0;
    background-color: black;
    z-index: 100;
    width: 960px;
}

http://www.ohchr.org。

$("#fixed-bar").css({
                "position" : "absolute",
                "left" : (($(window).width() - $("#fixed-bar").outerWidth()) / 2) + $(window).scrollLeft() + "px" })
               .hide();

$(window).scroll(function () {
    if ($(this).scrollTop() > 400) {
        $( #fixed-bar ).fadeIn(200);
    } else {
        $( #fixed-bar ).fadeOut(200);
    }
});

这样,不论屏幕大小,你的内容将横向集中。 Don tabes将j Query列入你的超文本。

更有办法集中使用 j子,例如将其置于绝对位置,寻找宽度,观察点宽度,并根据这些数字确定抵消点:

$( .mydiv ).css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签