简体   繁体   English

如何在具有填充的div中获取文本的位置

[英]how to get the position of text inside div having padding

I have a text inside the div which is having padding of some pixels, What i am trying to do is that i'm adding one more text just out of that div and indent the newly added text to the text inside div.我在 div 中有一个文本,它有一些像素的填充,我想要做的是我在该 div 之外再添加一个文本,并将新添加的文本缩进到 div 内的文本中。

Here in the jquery code i'm trying to get the position of the text inside the div, but i'm not able to get the actual position of the text inside the div, instead its giving the position of where the div actually starts.在 jquery 代码中,我试图获取 div 中文本的位置,但是我无法获取 div 中文本的实际位置,而是给出了 div 实际开始的位置。

How can i get the actual position of the text inside the div?如何获得文本在 div 中的实际位置?

here is what i have tried.这是我尝试过的。

 $(document).ready(function(){ console.log($('.banner-heading-text').position().top); console.log($('.banner-heading-text').position().left); });
 .banner-heading-text{ padding:10px 39px; color:red; font-size:3.4rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="banner-heading-text"> Here is the banner heading text </div>

You can't select the text and get the posiotion of it.您无法选择文本并获取它的位置。

The only way I see is to wrap the text with a span eg我看到的唯一方法是用跨度包裹文本,例如

 $(document).ready(function(){ alert($('.banner-heading-text span').position().top); alert($('.banner-heading-text span').position().left); });
 .banner-heading-text{ padding:10px 39px; color:red; font-size:3.4rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="banner-heading-text"> <span>Here is the banner heading text</span> </div>

You can't get the position value of a non-element.您无法获取非元素的位置值。 You either have to put a span inside the text.您要么必须在文本中放置一个跨度。

 <div class="banner-heading-text">
    <span>Here is the banner heading text<span>
 </div>

And do并且做

$(document).ready(function(){
    console.log($('.banner-heading-text > span').position().top);
    console.log($('.banner-heading-text > span').position().left);
});

or else add the padding in the resulting position value或者在结果位置值中添加填充

$(document).ready(function(){
    var paddingLeft = $('.banner-heading-text').css('padding-left');
    var paddingTop = $('.banner-heading-text').css('padding-top');
    console.log($('.banner-heading-text').position().top + paddingTop);
    console.log($('.banner-heading-text').position().left + paddingLeft);
});

您可以将填充值添加到 div 位置:

$('.banner-heading-text').css('padding-left') + $('.banner-heading-text').position().left

 $(document).ready(function(){ $banner = $('.banner-heading-text'); const topPos = $banner.position().top; const leftPos = $banner.position().left; const topPadding = parseInt($banner.css('paddingTop')); const leftPadding = parseInt($banner.css('paddingLeft')); const textPositionTop = topPos + topPadding; const textPositionLeft = leftPos + leftPadding; console.log(textPositionTop, textPositionLeft); });
 .banner-heading-text{ padding:10px 39px; color:red; font-size:3.4rem; }
 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <div class="banner-heading-text"> Here is the banner heading text </div>

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

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