jQuery .position()是否可以获取calc()值而不是px值? [英] jQuery .position() can I get the calc() value instead of the px value?

查看:113
本文介绍了jQuery .position()是否可以获取calc()值而不是px值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,可将div的大小从200px-290px调整为

I have a button that resizes a div from 200px - 290px

在此div中存在使用calc(50% - 25px);定位的元素 25px = half width of child-因此它们位于居中位置.

In this div elements exist that are positioned using calc(50% - 25px); 25px = half width of child - so they are positioned centrally.

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

I'm using jQuery's ui.swappable to swap elements, but the issue you is they seem to take on their calculated value permanently once swapped

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

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

Is there a way to swapping the calc(50% - 25px); instead of the value post calc?

$(this).position()

推荐答案

解决了这个问题.

我使用以下方法从可拖动的droppable div中提取子html,而不是交换元素的位置,然后将其附加到其各自的div中,从而保持calc()值.

Instead of swapping the positions of the elements, I used the following to extract the child html from the draggable droppable div's, and then append it into their respective div's, thus maintaining the calc() value.

$( ".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

Note: requires jQueryUI

这篇关于jQuery .position()是否可以获取calc()值而不是px值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆