简体   繁体   English

怎么能得到div位置根据百分比?

[英]how can get div position Based on the percentage?

whit this code you can get position a div based on pixel. 使用此代码,您可以根据像素获得div的位置。

$('#div').position();

but how can give div position Based on the percentage? 但如何根据百分比给出div位置?

Use window size. 使用窗口大小。

var position = $('#div').position();
var percentLeft = position.left/$(window).width() * 100;
var percentTop = position.top/$(window).height() *100;

This code will give you the position percentage for the top and left sides. 此代码将为您提供顶部和左侧的位置百分比。

This extends jQuery with a 'getWidthInPercent' function: 这扩展了jQuery的'getWidthInPercent'功能:

(function ($) {

    $.fn.getWidthInPercent = function () {
        var width = (
                        parseFloat($(this).css('width'))
                    +   parseFloat($(this).css('padding-right'))    
                    +   parseFloat($(this).css('padding-left')) 
                    ) / parseFloat($(this).parent().css('width'));
        return Math.round(100*width);
    };

})(jQuery);

Used as: 用作:

$('#element').getWidthInPercent() ;

jQuery position() : jQuery position()

Description: Get the current coordinates of the first element in the set of matched elements, relative to the offset parent. 描述:获取相对于偏移父元素的匹配元素集中第一个元素的当前坐标。

It is not used to set the position. 它不用于设置位置。 If you want to set the position (in percentage or otherwise) you need to use .css() to change its margin (or top, left), etc.. 如果你想设置位置(以百分比或其他方式),你需要使用.css()来改变它的边距(或顶部,左边)等。

I think you mean "get" instead of "give". 我认为你的意思是“得到”而不是“给予”。 You'd have to calculate the percentage unless it is in the CSS already. 你必须计算百分比,除非它已经在CSS中。

You can always experiment. 你总是可以试验。

#d {
    position: absolute;
    left: 33%;
    top: 50px;
    background-color: red;
    width: 100px; height: 100px;
}

<div id="d"></div>

alert($("#d").position().left + " " + $("#d").position().top);
alert($("#d").css("left") + " " + $("#d").css("top"));
var pcLeft = 100 * $("#d").position().left / $(window).width();
var pcTop = 100 * $("#d").position().top / $(window).width();
alert(pcLeft + " " + pcTop);

This solution has solved bug which @naziiiii was getting and also might help this question. 这个解决方案已经解决了@naziiiii获得的bug,也可能有助于解决这个问题。 check the below given link 检查下面给出的链接

$(function(){
 var percentLeft = '';
var percentTop = '';
   var percentLeft2 = '';
    var percentTop2 = '';

    $( "#div1" ).draggable({ 
        containment : '#container',
        tolerance: 'touch',
        stop:function(event, info){
            var position = $(this).position();
            percentLeft =  position.left/$('#container').width() * 100;
            percentTop =  position.top/$('#container').height() *100;       
        }
    });        
       $( "#div2" ).draggable({ 
        containment : '#container',
        tolerance: 'touch',
        stop:function(event, info){
            var position2 = $(this).position();
            percentLeft2 = position2.left/$('#container').width() * 100;
            percentTop2 =  position2.top/$('#container').height() *100;                             
        }
    });

   function wResize() {

         var winW = $('#container').width();
         var d = $('#div1').position();
    var d2 = $('#div2').position();      

         $('#div1').css({color:'green',
                        position: 'absolute',
                        left: percentLeft + '%',
                        top: percentTop + '%'
                        }); 
          $('#div2').css({color:'red',
                        position: 'absolute',
                        left: percentLeft2 + '%',
                        top: percentTop2 + '%'
                        });
        //$('#test2').html(percentLeft+ ' _____ '+ percentTop + 'winW : ' + winW);  
 }

 wResize();

$(window).resize(function() {
    wResize();
});
});    

http://jsfiddle.net/komal10041992/q74vxcxf/ http://jsfiddle.net/komal10041992/q74vxcxf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM