简体   繁体   English

宽度变化-粘性Div

[英]Width Changing - Sticky Div

I've implemented a sticky contact form, which works great. 我已经实施了一个粘性联系表格,效果很好。 But there is an annoying feature where it changes the width. 但是有一个令人讨厌的功能,它可以改变宽度。 I've tried all sorts of width properties such as auto, inherit, initial but still cannot manage to get the width to stay the same as it starts to scroll down! 我尝试了各种宽度属性,例如自动,继承,初始,但是仍然无法使宽度保持不变,因为它开始向下滚动!

Here is the Javascript 这是Javascript

<!-- Javascript Files -->
<script type="text/javascript">
$(window).load(function(){
$(function() {
  var a = function() {
    var b = $(window).scrollTop();
    var d = $("#scroller-anchor").offset().top;
    var c=$("#scroller");
    if (b>d) {
      c.css({width:"initial",position:"fixed",top:"30px"})
    } else {
      if (b<=d) {
        c.css({width:"100%",position:"relative",top:""})
      }
    }
  };
  $(window).scroll(a);a()
});
});//]]> 
</script>

Here is the scroller div: 这是滚动条div:

<div id="scroller-anchor"></div> 
  <div id="scroller" style="margin-top:10px;"> 

There is the contact form below this, but not needed for the example code! 下面有联系表,但示例代码不需要联系表!

Any help would be great, spent far to long on this with no success! 任何帮助都将是巨大的,为此花费了很长的时间而没有成功!

We'll store the initial width of the contact form, and assign the same width once you scroll and the div is fixed positioned. 我们将存储联系表单的初始宽度,并在滚动并且div固定定位后分配相同的宽度。

<script type="text/javascript">
$(window).load(function(){
    $(function() {
      var a = function() {
        var $win = $(window);
        var b = $win.scrollTop();
        var d = $("#scroller-anchor").offset().top;
        var c=$("#scroller");

        //store initial width
        var init_width = c.width();

        if (b>d) {
          c.css({width:init_width,position:"fixed",top:"30px",right:($win.width()-c.offset().left-init_width)})
        } else {
          if (b<=d) {
            c.css({width:"auto",position:"relative",top:"auto",right:"auto"})
          }
        }
      };
      $(window).scroll(a);a()
    });
});//]]> 
</script>

Notes about this: it will need to be readjusted anytime the width of the browser changes, and could go over the content if a user scrolls and then resizes the browser window to be smaller. 关于此的注意事项:每当浏览器的宽度改变时,都需要重新调整它,并且如果用户滚动然后将浏览器窗口的大小调整为较小,则可能会覆盖内容。

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

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