简体   繁体   English

根据两个元素的重叠宽度动态设置左填充

[英]Dynamically set padding-left based on overlap width of two elements

I've got a responsive site I'm building Where I have two elements that overlap each other. 我正在建立一个响应式网站,其中两个元素相互重叠。 THe size of the elements will change depending on the browser width as will the overlap and consequently I need to set left-padding on the right element dynamically. 元素的大小将根据浏览器的宽度而变化,重叠部分也将随之变化,因此,我需要在右边的元素上动态设置左填充。

I'm unsure of how to proceed with this. 我不确定如何进行此操作。 Have set up a Fiddle here . 在这里设了小提琴

html: 的HTML:

    <div class="container">

<div class="row copy intro">
            <section class="red">
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor illum nobis ullam neque, harum, magni. Reprehenderit veritatis in deleniti incidunt dolore dolores ex id expedita.</p>
                <p>Corporis soluta ducimus ut quasi libero nesciunt, eligendi autem, consequatur error sapiente labore, officia tempora in voluptas non deleniti veniam officiis, quis vero consequuntur quia!</p>


            </section>

            <section class="white">
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor illum nobis ullam neque, harum, magni. Reprehenderit veritatis in deleniti incidunt dolore dolores ex id expedita.</p>

                <p>Corporis soluta ducimus ut quasi libero nesciunt, eligendi autem, consequatur error sapiente labore, officia tempora in voluptas non deleniti veniam officiis, quis vero consequuntur quia!</p>

            </section>
    </div>
</div><!--container-->

css: CSS:

/* line 3, ../build/sass/_intro.scss */
.intro {
  background: #0079c2;
  position: relative;
  padding: 15px;
  padding-bottom: 150px;
}
/* line 9, ../build/sass/_intro.scss */
.intro section {
  position: relative;
  padding: 100px;
  width: 60%;
  -moz-border-radius: 500px;
  -webkit-border-radius: 500px;
  border-radius: 500px;
}

/* line 26, ../build/sass/_intro.scss */
.intro section.red {
  background: rgba(238, 45, 36, 0.85);
  color: #fff;
  z-index: 200;
}
/* line 31, ../build/sass/_intro.scss */
.intro section.red h1 {
  font-size: 24px;
}

/* line 45, ../build/sass/_intro.scss */
.intro section.white {
  background: #fff;
  color: #0079c2;
  position: absolute;
  top: 150px;
  right: 15px;
}

js: js:

// set intro sections width = height
$(document).ready(function() {
  var circleWidth= $('.intro section.red').outerWidth();

  $('.intro section').css('min-height', circleWidth + 'px');
  $('.intro section.white').css('width', circleWidth + 'px');
});

Thank you for your time. 感谢您的时间。

Use % for padding and adjust accordingly. 使用%进行填充并相应地进行调整。 See this revised Fiddle for an example. 有关示例,请参见此修订的Fiddle

The revised Fiddle comments out: 修改后的小提琴评论:

$('.intro section.white').css('width', circleWidth + 'px');

Fixing the width of the white circle means that it is not responsive any more. 固定白色圆圈的宽度意味着它不再响应。 If you need to do that for some reason, you would have to make adjustments. 如果出于某种原因需要这样做,则必须进行调整。

Here's a JSFiddle doing what I think you want: http://jsfiddle.net/6yro5vhx/2/ 这是一个JSFiddle,可以满足我的要求: http : //jsfiddle.net/6yro5vhx/2/

Basically I user offset() & outerWidth() on the two elements to work out the overlap, and then call calculatePadding() function on documentready & resize events. 基本上,我在两个元素上使用offset()和externalWidth()来计算出重叠部分,然后在documentready和resize事件上调用calculatePadding()函数。

  function calculatePadding() {
   var white = $('.intro section.white');
   var red = $('.intro section.red');
   var extraPadding = 20;
   var distanceLeft = white.offset().left;
   var redDistanceRight = red.offset().left + red.outerWidth();
   var paddingLeft = (redDistanceRight - distanceLeft) + extraPadding;

   $('.intro section.white').css('padding-left', paddingLeft + 'px');    
  }

Update the answer below mine is a far better way to achieve what you're looking for. 在我的下方更新答案是实现您想要的更好的方法。 CSS is a much better responsive approach than excess JQuery. 与多余的JQuery相比,CSS是一种更好的响应方法。

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

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