用jQuery修复了包装

时间:2017-06-11 08:46:50

标签: javascript jquery html css

在我的页面中,我有一个div(固定换行),我想在滚动后移动。 我尝试使用jQuery,但是div的高度太高而且超过了页脚。

img1

img2

这是我的代码:

抱歉,编辑2:



var elementPosition = $('#fixed-wrapper').offset();
$(window).scroll(function() {
  if ($(window).scrollTop() > elementPosition.top) {
    $('#fixed-wrapper').css('position', 'fixed').css('top', '0').css('margin', '20px 1%');
  } else {
    $('#fixed-wrapper').css('position', 'static');
  }
});

#header {
	width: 101%;
	padding: 10px 0px 0px;
	margin: -10px -10px 10px -10px;
	background-color: rgba(0, 0, 0, 0.7);
	display: table;
	min-width: 700px;
}

#main-bets{
	display: table;
	float: left;
	width: 68%;
	margin-left: 7%;
	margin-top: 20px;
	margin-bottom: 30px;
	min-width: 900px;
	max-width: 900px;
	background-color: rgba(0, 0, 0, 0.5);
}

#fixed-wrapper {
    	display: table;
    	float: right;
    	width: 22%;
    	right: 5px;
    	margin: 20px 1%;
    	max-width: 300px;
}

#footer {
	width: 101%;
	padding: 10px 0px 0px;
	margin: 20px -10px -10px -10px;
	background-color: rgba(0, 0, 0, 0.7);
	display: table;
	clear: both;
}

<div id="header">
    ...
</div>

<div id="main-bets
    ...
</div>

<div id="fixed-wrapper">
	....
</div>

<div id="footer">
	...
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以使用我的sticky float jQuery插件(demo page),或者使用相对较新的CSS属性:position:sticky(旧版Egde / IE中不是supported

答案 1 :(得分:0)

立即检查:

body {padding:0; margin: 0;}

#header {
    width: 101%;
    padding: 10px 0px 0px;
    margin: -10px -10px 10px -10px;
    background-color: rgba(0, 0, 0, 0.7);
    display: table;
    min-width: 700px;
}

#main-bets{
    float: left;
    width: 68%;
  margin-left: 7%;
    margin-top: 20px;
    margin-bottom: 30px;
    min-width: 900px;
    max-width: 900px;
    background-color: rgba(0, 0, 0, 0.5);
}

#fixed-wrapper {
  float: left;
  background: red;
  width: 22%;
  right: 5px;
  margin: 20px 1%;
  max-width: 300px;
}

#footer {
    width: 101%;
    padding: 10px 0px 0px;
    margin: 20px -10px -10px -10px;
    background-color: rgba(0, 0, 0, 0.7);
    clear: both;
}

// ---------------- HTML ---------- //

<div id="header">
    Header
</div>

<div id="main-bets">
    Bets
</div>

<div id="fixed-wrapper">
    Wrapper
</div>

<div id="footer">
    Footer
</div>