1

このようなページがあります

-----------------------------------
|          *************          |          
|          *************          |          
|          *************          |          
|          *************          |          
|          *************          |          
|          *************          |
-----------------------------------

外側のフレームはブラウザの端で<body>あり、星は要素の中央に配置された背景画像<body>です。

ここで、javascript を使用して、その背景を拡張するために、絶対に配置された DIV をその背景の両側に追加したいと思います。

-----------------------------------
|*********|*************|*********|          
|*********|*************|*********|          
|*********|*************|*********|          
|*********|*************|*********|          
|*********|*************|*********|          
|*********|*************|*********|
-----------------------------------

ウィンドウのサイズが変更されると、中央の背景が移動し (ウィンドウの中央に留まるように)、結果として両側の div が移動する必要があります。

誰でもこの問題を解決する方法を提案できますか?

なぜ私がこれをしているのかについての補足:

背景は大きな画像です。ここで説明するように、反転して画面全体に繰り返し表示したいと考えています。

Javascript を使用した背景反転タイリング

反転の実際のプロセスはその質問で説明されていましたが、この質問はポジショニングの CSS/Javascript の側面を扱っています。

対応していないブラウザの場合は、最初の図のようにページをそのままにして、背景だけを中央に配置することに注意してください。

4

3 に答える 3

1

You can try a pure css solution using positioning. I am assuming that you have a fixed-size image in the centre of the page. For example's sake let's say your image is 800px wide, then

  • Position the left div with left 0px, right 50% and add a right margin of 400px
  • Position the right div with left 50%, right 0px and add a left margin of 400px

Example: http://jsfiddle.net/F4kay/

(note that I have assumed a smaller image width 256px for the smaller jsfiddle window)

CSS:

#left {
    position:absolute;
    left:0px;
    right:50%;
    top:0px;
    bottom:0px;
    margin-right: 128px; /* image width / 2 */
    background-color:#ccc;
}    

#right {
    position:absolute;
    left:50%;
    right:0px;
    top:0px;
    bottom:0px;
    margin-left: 128px; /* Image width / 2 */
    background-color:#ccc;  
}

​<strong>HTML:

<div id="left">Left</div>
<div id="right">Right</div>​

As for the height of these divs, it's up to you how you deal with this, either top / bottom: 0px or a fixed/percentage height. In the example I have used top 0px and bottom 0px.

The trick is to basically add 2 divs, one which takes up the left half of the screen and another which takes up the right. You add a margin to the inner div edges to reveal the inner contents. I have assumed a fixed width but you could also use half of a percentage width. If your image changes dynamically with js it's just a case of updating the margins.


For completeness, I've included a full example using this technique. I think it's a clean solution.

Example: http://jsfiddle.net/Hqpyx/

CSS:

body, .bgImage {
    background-image: url('http://flickholdr.com/640/1280/5');
}

.flip {
    -moz-transform:    scaleX(-1);
    -o-transform:      scaleX(-1);
    -webkit-transform: scaleX(-1);
    transform:         scaleX(-1);
    filter:            FlipH;
    -ms-filter:        "FlipH";
}

body {
 background-position: top center;     
}

#left {
    position:absolute;
    left:0px;
    right:50%;
    top:0px;
    bottom:0px;
    margin-right: 320px; /* image width / 2 */
    background-position:top left;    
}


#right {
    position:absolute;
    left:49.99%;
    right:0px;
    top:0px;
    bottom:0px;
    margin-left: 320px; /* Image width / 2 */
    background-position:top right;
}

HTML:​</p>

<div id="left" class="bgImage flip"></div>
<div id="right" class="bgImage flip"></div>​

Personally I would avoid flipping the image like this. Unless you have some restriction you could just edit your background image and splice half and half of a flipped version together like

[ ] = full image

{ } = flipped image

create an image that looks like

}[ ]{ 
于 2012-11-08T11:24:42.550 に答える
1

JavaScript を使用して 2 つの DIV を追加します。画面の中心に対して必要に応じて位置を設定します。ブラウザ ウィンドウのサイズが変更されたときに DIV の位置を変更する onresize イベントを追加するよりも。

window.onresize = updateDivsPosition;
于 2012-11-08T11:16:57.657 に答える
0

これが私の静的CSSソリューションです。マットの答えに似ていますが、反転した画像と柔軟な幅で動作させることができませんでした。

ここでデモ。

これまでのところ、両側に 1 つの div しか追加されていません。さらに必要な場合は、そのための JS を作成できます。ページの HTML マークアップは次のとおりです。

<div class="content">
    <div class="left-background"></div>
    <div class="right-background"></div>
    content
</div>

およびCSS:

body {
    color: #dddddd;
    overflow-x: hidden;
}
@media (max-width: 400px) {
    body {
        overflow-x: visible;
    }
    .left-background, .right-background {
        display: none;
    }
}
.content, .left-background, .right-background {
    background-image: url(http://flickholdr.com/400/300/sea,sun/bw);
    height: 200px;
    width: 400px;
}
.left-background, .right-background {
    -moz-transform:    scaleX(-1);
    -o-transform:      scaleX(-1);
    -webkit-transform: scaleX(-1);
    transform:         scaleX(-1);
    filter:            FlipH;
    -ms-filter:        "FlipH";

    position: absolute;
}
.left-background {
    left: -400px;
}
.right-background {
    right: -400px;
}
.content {
    margin: 0 auto;
    position: relative;
}
于 2012-11-08T12:12:52.983 に答える