滚动后,使用响应宽度修复div位置

时间:2017-02-14 11:56:34

标签: jquery css

我的目标是在某些滚动后显示div作为位置固定,并以响应宽度显示div元素。

在下面的示例中,脚本可以正常工作,但我希望与橙色元素具有相同的宽度(响应)。

.fixed {
  position: fixed;
  top: 0px;
  z-index': 10000;  
}

Example

3 个答案:

答案 0 :(得分:0)

将拼写错误修正为z-index并将宽度增加到420px;

.fixed {
  position:fixed;
  top: 0px;
  z-index: 10000;
  width: 420px;
 }

演示:http://jsfiddle.net/y5fo1emc/33/

答案 1 :(得分:0)

我无法找到响应宽度的CSS解决方案。 RahulB的答案有效但只使用固定宽度。这是一个响应式JS解决方案,在窗口调整大小上添加了一个函数:

function resizeFixed() {
    var orWidth = $('.wrapper .group .span_4_of_4').width();
    $('.fixed').css('width', orWidth);
}

演示:http://jsfiddle.net/Treeindev/4k93t136/1/

答案 2 :(得分:0)

试试这个

<section id="mainContainer">

<div class="section group">
    <div class="col span_4_of_4">
 <img src="http://placehold.it/500x150">  


  </div>

  <div class="box-inner">
  <div class="section group wrapper">
    <div class="col span_4_of_4 box">
 <h1>
 I will be fixed after scrolling
 </h1>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

  </div>


<div class="section group">
    <div class="col span_4_of_4">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.   

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.   

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.   


  </div>
  </div>
  </div>
  </section>



    $(document).ready(function () {

    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('.box-inner').addClass('fixed');
        } else {
           $('.box-inner').removeClass('fixed');
        }
    });
});

现场演示 - http://jsfiddle.net/grinmax_/y5fo1emc/37/