2017-08-02 44 views
1

我有一個backoffice,我可以將卡添加到傳播並定位它們,但是我喜歡使用topleft。然後我必須在我的網站上展示這些點差。絕對中心動態數divs

我的問題是,我想總是水平和垂直卡中的點差。主要問題是每個傳播中的卡片數量是可變的,並且每個卡片topleft根據用戶的意願而不同。

我將每張卡片的topleft保存在數據庫中,然後將它們呈現在網站中。

這裏是檢查出的小提琴:https://jsfiddle.net/wajrga88/10/

任何幫助嗎?我甚至可能問什麼?

編輯:我更新了一個更接近現實場景的小提琴。

+0

https://jsfiddle.net/kym60L9g/ – bhv

+0

如果你正在使用的位置是絕對的,你必須使用: 試試這個左還是右。而不是px,你可以使用%,所以它可以在任何屏幕上自動調整 –

+0

可能你可以使用flexbox來獲得你的結果,正如@bhv所建議的那樣,但爲了進一步的幫助,我希望看到一個帶有最終想要結果的jpg。另外,如果可以,請考慮減少標記。 – Stratboy

回答

1

這只是真的可能與JavaScript,因爲你需要計算這個邊界框。通過調用setCardsPosition(CONTAINER_ELEMENT,POSITONS_ARRAY)

//define dimensions of a deck - this is necessary for centering since the registration point is on the top-left corner of the deck 
 
var deck_width = 54.6 
 
var deck_height = 98.6 
 

 
function setCardsPosition(target,positions){ 
 
\t // calculate bounding box 
 
\t var minX = 9999 
 
    var minY = 9999 
 
    var maxX = -9999 
 
    var maxY = -9999 
 
    for(i=0;i<positions.length;i++){ 
 
    //this will find the most left (minX) most right (maxX) most up (minY) and most down (maxY) position of all the cards. 
 
    \t minX = Math.min(minX,positions[i][0]) 
 
    minY = Math.min(minY,positions[i][1]) 
 
    maxX = Math.max(maxX,positions[i][0]) 
 
    maxY = Math.max(maxY,positions[i][1]) 
 
    } 
 
    //calculate width&height of min-max coordinates - the bounding box (x=minX,y=minY,w=bounds_width,h=bounds_height) 
 
    var bounds_width = maxX-minX 
 
    var bounds_height = maxY-minY 
 
    
 
    //get width&height of the container 
 
    var target_width = target.clientWidth 
 
    var target_height = target.clientHeight 
 

 
    //get all 3 cards in container...you could create them dynamically aswell at this point 
 
    var cards = target.getElementsByClassName('transparent_deck') 
 
    for(i=0;i<positions.length;i++){ 
 
    \t //calculate cards position using the bounding box and the defined deck dimensions 
 
    // target_width/2 -> center point of container 
 
    // + positions[i][0] -> add raw position of current card 
 
    // - minX -> move card so the most left card lies on the center of the container 
 
    // - bounds_width/2 -> move card half the width of the bounding box of all cards 
 
    // - deck_width/2 -> correct the cards position (due to the registration point is top-left for css absolute positions) 
 
    \t var cardX = target_width/2 + positions[i][0] - minX - bounds_width/2 - deck_width/2 
 
    var cardY = target_height/2 + positions[i][1] - minY - bounds_height/2 - deck_height/2 
 
    //set position to style 
 
    cards[i].style.left = cardX + 'px' 
 
    cards[i].style.top = cardY + 'px' 
 
    } 
 
} 
 

 
var positions1 = [[80,100],[150,140],[220,100]] // the positions of each card 
 
setCardsPosition(document.getElementById('spread1'),positions1)
body{ 
 
    background-color: #666666; 
 
} 
 
.transparent_deck{ 
 
\t position: absolute; 
 
\t width: 54.6px; 
 
\t height: 98.6px; 
 
\t border-radius: 5px; 
 
    background: rgba(255,255,255,0.5); 
 
\t border: solid 2px #ffffff; 
 
} 
 

 
.spread{ 
 
    height: 275px; 
 
    position: relative; 
 
    border: 2px solid #000; 
 
}
<div class="row"> 
 
    <div class="col-xs-6 spread" id="spread1"> 
 
    <div class="transparent_deck" style="top: 100px; left: 80px;"></div> 
 
    <div class="transparent_deck" style="top: 140px; left: 150px;"></div> 
 
    <div class="transparent_deck" style="top: 100px; left: 220px;"></div> 
 
    <div class="row"> 
 
     <div class="col-xs-12"> 
 
     <p class="spread_title">Title</p> 
 
     <p class="spread_description">Description</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

您可以設置位置

+0

嘿。我將該代碼應用於當前情況,但恐怕它不起作用100%:/以下是我當前的場景:https://jsfiddle.net/ddgtmj43/1/ –

+0

這是因爲您甚至使用jquery儘管它不包括在內。此外,這些頭寸也有點混亂。 https://jsfiddle.net/md47ncnm/ –

+0

我的錯誤,解決方案是。我忘記考慮到在我的辦公室裏,卡片的大小几乎是我在網站上的三倍,所以我不得不將每個頂部分開,然後左移3,以使其成比例。它現在工作100%。謝謝:D(如果可能的話,你能在代碼中的評論中更詳細地描述一下嗎?我真的很想了解它的每一部分,所以我可以增長一點:p) –