简体   繁体   English

固定div位置时的jQuery Offset问题

[英]JQuery Offset issue when div position is fixed

I have a website which has a page layout and style something like mentioned in this JsFiddle 我有一个网站,该网站的页面布局和样式类似于此JsFiddle中所述

Now Using JQuery when I click on the button, content is being displayed properly as shown below: 现在,当我单击按钮时使用JQuery时,内容将正确显示,如下所示:

在此处输入图片说明

But when I first scroll the page and then click the button, content is not displaying properly as shown: 但是,当我第一次滚动页面然后单击按钮时,内容无法正确显示,如下所示:

在此处输入图片说明

Can you please guide me to handle this situation ? 您能指导我处理这种情况吗?

I have used below jQuery for this. 我已经在jQuery下面使用过此功能。 But it seems offset or position is not working 但似乎offsetposition不起作用

$('#btn').click(function(){
    var t = $(this).offset();
  console.log(t);
  $('.control-container').css('top', t.top + 20 + 'px');
  $('.control-container').css('display', 'block');
});

$(document).on('scroll', function(){
$('.control-container').css('display', 'none');
});

There is no need to specifically mention position property here. 此处无需专门提及position属性。

Also remove the closing a tag and replace it with </button> 也除去封闭a标签,并用其替换</button>

Currently container is occupying full width ,but that can also be set 当前容器正在占用全宽,但也可以设置

 $('#btn').click(function() { var t = $(this).offset(); console.log(t); $('.control-container').css('top', t.top + 30 + 'px'); $('.control-container').css('display', 'block'); }); $(document).on('scroll', function() { $('.control-container').css('display', 'none'); }); 
 .header { background-color: maroon; color: #fafafa; height: 60px; position: fixed; width: 100%; text-align: center; line-height: 19px; font-size: 25px; z-index: 2; padding: 0px; margin: 0px; top: 0; } .content { background-color: #fff; border: 1px solid #999; padding: 5px; height: 100%; width: 100%; position: absolute; z-index: 1; top: 60px; } .control-container { width: auto; background-color: red; #position: fixed; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="header"> Header </div> <div style="clear:both"> </div> <div class="content"> <br/> <br/> <br/> <br/> <br/> <button id="btn">Click Me</button> <div class="control-container" style="display:none;"> Keep me exactly underneath 'Click Me' when Page is scrolled. </div> </div> </div> 

You don't need to use offset to achieve that... And if you need to keep CSS with position:fixed , you need to switch it in javascript to static . 您无需使用offset即可实现这一点...并且,如果需要保持CSSposition:fixed ,则需要在javascript中将其切换为static

The thing you are looking for is simply display:table ... 您正在寻找的东西只是display:table ...

$('#btn').click(function(){
  $('.control-container').css({'display': 'table','position': 'static'});
});

$(document).on('scroll', function(){
   $('.control-container').css({'display': 'none','position': 'fixed'});
});

Check out this JSFiddle 看看这个JSFiddle


But if you really need a solution with position:fixed based on button position, you should try this way: 但是,如果您确实需要根据button位置position:fixedposition:fixed的解决方案,则应尝试以下方式:

$('#btn').click(function(){
    var button_fixed_position = $('#btn').get(0).getBoundingClientRect();
  $('.control-container').css({'display': 'block','left' : button_fixed_position.left, 'top' : button_fixed_position.bottom});
});

$(document).on('scroll', function(){
   $('.control-container').css({'display': 'none'});
});

Check out second JSFiddle 查看第二个JSFiddle

CSS position fixed property positions an element referencing view's/body's dimension. CSS position fixed属性可定位引用视图/主体尺寸的元素。

If you have access of modifying CSS , then just remove the position: fixed; 如果您有权修改CSS ,则只需移除position: fixed; property from .control-container . .control-container属性。

If you don't have access, then using script add position: static !important property to .control-container . 如果您没有访问权限,请使用脚本在.control-container添加position: static !important属性。

$('.control-container').css('cssText', 'position: static !important');

Modified JSFiddle 修改后的JSFiddle

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

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