【问题标题】:jQuery .position() can I get the calc() value instead of the px value?jQuery .position() 我可以得到 calc() 值而不是 px 值吗?
【发布时间】:2017-03-16 12:42:31
【问题描述】:

我有一个按钮可以将 div 的大小从 200px 调整到 290px

在这个 div 元素中存在使用calc(50% - 25px); 定位的元素 25px = half width of child - 所以它们位于中心位置。

我正在使用 jQuery 的 ui.swappable 交换元素,但您的问题是,一旦交换,它们似乎会永久采用其计算值

预交换left:calc(50% - 25px); 交换后left:74px

有没有办法交换 calc(50% - 25px);而不是计算后的值?

$(this).position()

【问题讨论】:

  • 请在此处发布您的代码,以便我们提供帮助。

标签: jquery css position


【解决方案1】:

解决了这个问题。

我没有交换元素的位置,而是使用以下内容从可拖放的 div 中提取子 html,然后将其附加到各自的 div 中,从而保持 calc() 值。

$( ".ClassofSwappableElement" ).draggable({
    stack: ".ClassofSwappableElement",
    distance: 0,
    revert: "invalid", 
    opacity: 0.5, 
    helper: "clone" 
});

$( ".ClassofSwappableElement" ).droppable({
    accept: ".ClassofSwappableElement",
    drop: function( evt, ui ) {

var draggable = ui.draggable,
    draggablebgc = ui.draggable.children().css('background-color'),
    draggablevalue = ui.draggable.attr('value'),
    droppable = $(this),
    droppablebgc = $(this).children().css('backgroundColor'),
    droppablevalue = $(this).attr('value');

draggable.attr('value', droppablevalue );
droppable.children('.ClassofChildYouWanttoChangeBgColorof').css({ backgroundColor: draggablebgc });
droppable.attr('value', draggablevalue );
draggable.children('.ClassofChildYouWanttoChangeBgColorof').css({ backgroundColor: droppablebgc });

var drophtml = droppable.html(),
    draghtml = draggable.html();

droppable.empty();
draggable.empty();
droppable.append(draghtml);
draggable.append(drophtml);

注意:需要jQueryUI

【讨论】:

猜你喜欢
  • 2014-10-02
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多