2016-03-08 73 views
4

這是一個jQuery UI可排序/可拖動的問題,如果被拖動的元素使用margin:0 auto居中,則拖動將從容器的左側開始,而不是元素的中心。Draggable And Margin:0 auto;

繼承人演示顯示我的意思:http://codepen.io/anon/pen/YqWRvV。只需拖動紅色方塊。

如果您從'.drag'元素中刪除margin:0 auto,您將看到拖動正常開始。

如何在使用margin:0 auto居中時如何解決此問題並將其元素拖動到真正位置?

這似乎發生在Google Chrome中。

$("#c").sortable();

`.drag { 
    margin: 0 auto; 
    width: 200px; 
    height: 200px; 
    background-color: red; 
}` 

中心與CSS「鈣」元素修復該問題,但我的元素的寬度可以動態地改變。 'calc'也不如'margin'支持。

在容器中包裝可拖動可修復問題,但HTML更改不是我正在尋找的解決方案。

更新: 所以這不是一個jQuery的bug。這是谷歌瀏覽器的一個錯誤。顯然,錯誤的位置是由鉻檢索的,所以jQuery從chrome告訴它元素的位置開始拖動。我在控制檯中輸出元素的左側位置,當顯然不是13px時,可以看到它是13px。 http://codepen.io/anon/pen/YqWRvV。我想知道這個bug是否被報告過。

SOLUTION http://codepen.io/anon/pen/MyjeJP感謝朱利安·格雷 注意:此方法可能需要一種方法來刷新幫手位置。

+0

我使用FirefoxDeveloperEdition,我不知道我看到它工作在Firefox的bug – silviagreen

+0

。這意味着這是一個Chrome問題,在Chrome上嘗試它,你會看到。 – guub

回答

3

不同的Chrome比Firefox返回的位置,但你使用偏移,這是什麼用途排序正確的座標。問題是,在mouseStart,保證金從計算中刪除,這可能有意義,當你有一個保證金設置。但是,由於汽車利潤率的問題,

您可以添加一個選項來忽略邊距並修改_mouseStart。就像這樣:

$("#c").sortable({ 
    ignoreMargins: true 
}); 

$.ui.sortable.prototype._mouseStart = function(event, overrideHandle, noActivation) { 
    ... 
    if (!this.options.ignoreMargins) { 

     this.offset = { 
     top: this.offset.top - this.margins.top, 
     left: this.offset.left - this.margins.left 
     }; 
    } 
    ... 
} 

http://codepen.io/anon/pen/BKzeMp

編輯:

如果你不想修改插件,您可以用開始停止事件工作。一個問題是很難檢查邊距是否已設置爲自動,因此您可以在可分類調用中定義它們,這不像應該那樣靈活,或者您可以找到一種方法來檢查哪些邊距是自動動態的。

$("#c").sortable({ 
    start: function(e, ui) { 
    var marginsToSet = ui.item.data().sortableItem.margins; 
    // You set the margins here, so they don't go back to 0 
    // once the item is put in absolute position 
     ui.item.css('margin-left', marginsToSet.left); 
     ui.item.css('margin-top', marginsToSet.top); 

    }, 
    stop: function(e, ui) { 
    // Then you need to set the margins back once you stop dragging. 
    // Ideally this should be dynamic, but you have to be able 
    // to check which margins are set to auto, which as you'll 
    // see if you look for some answers on this site, 
    // doesn't look that simple. 
    ui.item.css('margin', '20px auto'); 
    } 
}); 

http://codepen.io/anon/pen/mPrdYp

+0

這可能是我正在尋找的解決方案,它似乎現在正在工作。看起來它正在改變很多核心jQuery的東西。這安全嗎?這會帶來什麼問題? – guub

+0

我遇到了一個問題。如果元素具有margin-top,那麼水平固定的東西現在是垂直的。 http://codepen.io/anon/pen/GZqbQv – guub

+0

只要你的保證金值不是0px,當處於絕對位置時,它會引起問題。這就是爲什麼我建議能夠使用選項切換它,因爲它在任何情況下都不起作用。也就是說,可能還有其他副作用,但通常只需進行最少的測試,您應該能夠看到它在實際情況下是否有效。至於安全性,您只能添加一個沒有發生的選項,並跳過一條語句。但是,這並不理想。隨着此更改,您始終可以創建另一個排序。或者嘗試使用啓動事件複製此行爲。 –

2

嘿,我正在測試你的Codepen並在兩個不同的瀏覽器(Mozilla & Chrome)中檢查過它。

我得到了你說的是Chrome的東西。但在Mozilla中它工作正常。

而在鉻位左邊的屬性值是不同的。在Mozilla的意思是它剩下的:560px,而拖動,並在Chrome中,它從左邊開始:0;

這就是從左側顯着顯示拖動元素的原因。

我希望它能幫助你。

1

更新您的jQuery添加克隆幫手:

$("#c").sortable({ 
helper: "clone" 
}); 

和更新您的CSS添加位置和溢出:

#c { 
    width: 100%; 
    padding: 50px 0px; 
    border: solid 5px; 
    box-sizing: border-box; 
    overflow: hidden; 
    position: relative; 
} 

Updated Codepen

+0

感謝這個解決方案,但它不是我正在尋找的,因爲我必須將我的許多元素包裝在一個容器中,並且該容器就是被拖動的東西。不是紅場本身。雖然這可能是一個很好的解決方案,未來其他人將陷入困境 – guub

1

很好的解決辦法我來到身邊是 http://codepen.io/anon/pen/VaaaaZ

使用calc代替汽車在CSS

+0

Calc不會自動將元素居中,無論寬度如何。它也沒有很好的瀏覽器兼容性。我將不得不繼續尋找 – guub

+0

我已經在Chrome和Firefox上測試過,它工作正常,如果以正確的方式編寫,calc會使元素居中,即'calc(100% - 元素/ 2的寬度)' – Cybersupernova

+0

它支持幾乎所有主流瀏覽器http://www.w3schools.com/CSSref/func_calc.asp – Cybersupernova

0

如果你不能設置中心與CSS比其他選項設置中心與jQuery。 請查閱我的代碼庫更新演示,根據您在本文和其他答案中解釋的反饋和要求進行更新。 Demo

$("#c").sortable(); 

$(".drag").css("margin-left",($(document).width()/2)-125); 
$(window).resize(function(){ 
    $(".drag").css("margin-left",($(document).width()/2)-125); 
}); 
0

似乎在Chrome .sortable()當您使用margin: 0 auto,只是設置爲0left位置不正確計算元素的位置。所以我想你應該找到其他方式來水平居中你的元素。


一種選擇是使用text-align: center父元素和display: inline-block兒童

$("#c").sortable();
#c { 
 
    width: 100%; 
 
    padding: 50px 0px; 
 
    border: solid 5px; 
 
    box-sizing: border-box; 
 
    text-align: center; 
 
} 
 

 
.drag { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: red; 
 
    display: inline-block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
 
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<div id="c"> 
 
    <div class="drag"></div> 
 
</div>

另一種是使用Flexbox和中心子元素與justify-content: center

$("#c").sortable();
#c { 
 
    width: 100%; 
 
    padding: 50px 0px; 
 
    border: solid 5px; 
 
    box-sizing: border-box; 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 

 
.drag { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
 
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<div id="c"> 
 
    <div class="drag"></div> 
 
</div>

+0

文字對齊方法可能會讓人不快看到,但我會等待更好的選項。問題不在於jQuery。它與JavaScript獲取元素的錯誤位置。 – guub

相關問題