简体   繁体   English

将元素粘贴到浏览器窗口/容器的底部

[英]Sticking element to the bottom of the browser window/container

UPDATE - Answered my own question, check it lower. 更新 - 回答了我自己的问题,检查一下。

I need to stick an element to the bottom of the browser window ( position: fixed; bottom:0; ) until the end of the container is reached. 我需要将一个元素粘贴到浏览器窗口的底部( position: fixed; bottom:0; ),直到到达容器的末尾。

Basically, it is an image of a person, that is cut off at the bottom. 基本上,它是一个人的形象,在底部被切断。 Once I scroll enough in the page to see the person's image, and scroll down to the last pixel of that image, it should become sticky to the bottom, until the end of the container is reached. 一旦我在页面中滚动到足以查看该人的图像,并向下滚动到该图像的最后一个像素,它应该变为粘到底部,直到到达容器的末尾。

Here is a jsfiddle of the content structure. 这是内容结构的jsfiddle。 https://jsfiddle.net/mdy41ens/2/ https://jsfiddle.net/mdy41ens/2/

So here, there is the top content of the page, and when you scroll down, there is a kitten image on the left. 所以这里有页面的顶部内容,当你向下滚动时,左边有一个小猫图像。 Both the kitten and the text is in the ".container". 小猫和文本都在“.container”中。 So while you scroll the page down to read all the text, the kitten image should stay fixed at the bottom of the browser window, until the end of the container is reached, and then its simply position: absolute; bottom:0; 因此,当您向下滚动页面以阅读所有文本时,小猫图像应保持固定在浏览器窗口的底部,直到到达容器的末尾,然后它的简单position: absolute; bottom:0; position: absolute; bottom:0; or something like that (stuck to the bottom of the container, not the browser anymore). 或类似的东西(粘在容器的底部,而不是浏览器)。

I found lots of examples of code helping do the opposite - sticking the element to the top of the screen, once the top of the browser window meets the top of the element being stickied. 我找到了许多代码帮助相反的例子 - 一旦浏览器窗口的顶部遇到被粘贴元素的顶部 ,就将元素粘贴到屏幕顶部。

<div class="container">
    <div class="imgContainer">
        <img src="http://upshout.com/wp-content/uploads/2015/06/dwarf-kitten-01.jpg" />
    </div>
    <div class="contentContainer">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras laoreet eget massa sit amet feugiat. In et nisi sagittis, condimentum leo vitae, congue purus. Aliquam nec malesuada odio, non varius orci. Suspendisse luctus ipsum a odio tristique tempus at at ante. Duis auctor lacus arcu, ut consequat libero fringilla sed. Ut in dapibus purus. Vivamus posuere sapien ut ex semper, ut pharetra nunc mollis. Vestibulum suscipit ipsum massa, a pharetra leo accumsan vel. Praesent varius ornare scelerisque. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin vel ligula quis dolor malesuada pulvinar at id magna. Nulla pretium erat ante. Cras fringilla ut nulla quis rutrum. Quisque eget ex auctor tortor varius tempor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam auctor tincidunt fermentum.</p>
        <p>Suspendisse tincidunt ac urna nec ultrices. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed risus orci, volutpat a nisl in, posuere blandit augue. Quisque nec dictum metus. Donec quam orci, vestibulum vitae erat vel, sodales consectetur dolor. Morbi sed leo sit amet nibh hendrerit ornare. Ut ac malesuada risus. Ut congue lorem ut justo accumsan, vel dignissim turpis varius.</p>
        <p>Vivamus et neque mauris. Fusce pulvinar pretium dui, quis auctor mi suscipit ut. Nam et turpis et purus posuere fermentum at ac libero. In sagittis aliquet urna, nec dapibus lorem convallis iaculis. Aliquam a suscipit sapien. Quisque eget velit quis ex placerat auctor. In sodales libero non viverra vehicula. Maecenas pellentesque lacinia pretium. Nam iaculis nunc tincidunt purus scelerisque, quis sodales nunc faucibus. Phasellus semper aliquet tristique. Pellentesque nec leo a sapien gravida maximus. Pellentesque non pellentesque eros. Quisque gravida tortor sed nibh tincidunt varius. In finibus facilisis congue. Nam posuere dui orci, et consequat ipsum pretium ut.</p>
        <p>Aenean commodo nisi et dapibus iaculis. Mauris mollis accumsan massa. Aliquam lectus enim, dictum eu dapibus vitae, feugiat non libero. Cras ac sapien euismod, facilisis justo vitae, accumsan metus. Aenean ultricies blandit ipsum nec semper. Vestibulum imperdiet enim convallis elit iaculis sollicitudin. Donec commodo, tortor vitae imperdiet dictum, dui purus elementum sem, vel rhoncus nisi felis vel nulla. Ut a massa id velit porta tincidunt at vel nunc. Sed vel eleifend nunc, a efficitur enim. Mauris quis odio id eros feugiat blandit nec eget velit. Nam sed magna eu tellus porta maximus sed eu ligula.</p>
    </div>
</div>

Forgive the fact that this is choppy. 原谅这是不稳定的事实。 I'm going to leave all the fancy styling to you, but this gives you the idea. 我将把所有花哨的造型留给你,但这会给你一个想法。 (I put the image on the right for now because getting the text to stay on the right with the image on the left wold require another div and some styling for it, and there is a space between the image and the bottom container which comes from some padding or something somewhere. Trivial really.) (我现在把图像放在右边,因为左边的图像让文字保持在右侧,需要另外一个div和一些样式,图像和底部容器之间有一个空间来自一些填充或某处某事。真的很琐碎。)

You can use JQuery to monitor the location of the scroll and once you have reached the bottom of the window, set a different style to the image such that it becomes position: fixed; 您可以使用JQuery来监视滚动的位置,一旦到达窗口的底部,为图像设置不同的样式,使其成为position: fixed; and bottom: 0; bottom: 0;

The real magic here is the JQuery. 这里真正的魔力是JQuery。

JQuery JQuery的

$(document).ready(function () {

    $(window).scroll(function () {
            var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
        if (scrollBottom < 400) {
            $('.imgContainerScrolling').addClass('imgContainerAtBottom');           
        } else {
                $('.imgContainerScrolling').removeClass('imgContainerAtBottom');
        }
    });
});

JSFiddle 的jsfiddle

EDIT 编辑

I think this solution is very close to the result you're expect. 我认为这个解决方案非常接近你期望的结果。 It needs some CSS and script adjustment to make it fully fluid and clean but you've got here a way to do it. 它需要一些CSS和脚本调整,使它完全流畅和清洁,但你有一个方法来做到这一点。

See it here 在这里看到它

JS JS

$(function(){
    topKitten = $('.imgContainer').offset().top;
  topKittenHeight = $('.imgContainer').height();
  topContainer = $('.contentContainer').offset().top;
  bottomContainer = $(document).height() - topContainer - 2*topKittenHeight ;
  $(window).scroll(function(){
    windowScroll = $(window).scrollTop();
    if(windowScroll > topKitten + 220 && windowScroll < bottomContainer){
        $('.imgContainer').addClass('fixed').removeClass('bottom');
    }else if (windowScroll > bottomContainer){
        $('.imgContainer').removeClass('fixed').addClass('bottom');
    }else{          $('.imgContainer').removeClass('fixed').removeClass('bottom');
    }
  });
});

CSS CSS

.container { overflow: hidden; width: 100%; position: relative;  }
.imgContainer.fixed { position: fixed; width: 45%; top: 100%; transition: all 0.5s; margin-top: -220px;}
.imgContainer.bottom { position: absolute; top: 100%; margin-top: -220px; transition: all 0.5s;}
.imgContainer { position: absolute; top: auto; margin-top: 0; transition: all 0.5s;}

Ok, I figured it out with this code. 好的,我用这段代码搞清楚了。 It can be more beautiful, but at least it works well. 它可以更漂亮,但至少它运作良好。 Pat and Joe are parts of an interactive slider. Pat和Joe是交互式滑块的一部分。 If Pat is selected, Pat's onscroll events get triggered, if Joe - Joes, if other slider members, then nothing gets triggered, because only these two images are smaller (height wise) than the text on the side, therefore you dont need to sticky other images to the bottom, because they are already at the bottom. 如果Pat被选中,Pat的onscroll事件会被触发,如果Joe-Joes,如果是其他滑块成员,那么什么都不会被触发,因为只有这两个图像比侧面的文本更小(高度明智),因此你不需要粘其他图像到底部,因为它们已经在底部。

.stickit has fixed css styling to stick to the bottom of the browser window. .stickit具有固定的CSS样式以粘贴到浏览器窗口的底部。

.stickit-end has absolute css styling to stick to the bottom of the container. .stickit-end具有绝对的CSS样式,可以粘贴到容器的底部。

The content is dynamic, so I need to recalculate the heights of elements on scroll, rather than on page load. 内容是动态的,所以我需要重新计算滚动元素的高度,而不是页面加载。 I hope this helps someone. 我希望这可以帮助别人。

  $(function(){
    var topImage = $('.slayerItemPicture').offset().top;
  var topImageHeight = $('.slayerItemPicture').height();
  var scrollBottom = topImage - $(window).height() + topImageHeight;
    var bottomContainer = $("#CULTURE").offset().top - $(window).height();
  $(window).scroll(function(){
    if ($(".Joe").hasClass("activeItem")) {
            $('.Pat .slayerItemPicture').removeClass('stickit-end');
      $('.Pat .slayerItemPicture').removeClass('stickit');
      topImageHeight = $('.Joe .slayerItemPicture').height();
    scrollBottom = topImage - $(window).height() + topImageHeight;
    windowScroll = $(window).scrollTop();
      bottomContainer = $("#CULTURE").offset().top - $(window).height();
    if(windowScroll > scrollBottom && windowScroll < bottomContainer){
        $('.Joe .slayerItemPicture').addClass('stickit');
      $('.Joe .slayerItemPicture').removeClass('stickit-end');
    }
      else if (windowScroll >= bottomContainer) {
      $('.Joe .slayerItemPicture').removeClass('stickit');
      $('.Joe .slayerItemPicture').addClass('stickit-end');
      }
      else {
      $('.Joe .slayerItemPicture').removeClass('stickit-end');
      $('.Joe .slayerItemPicture').removeClass('stickit');
    }
    }
    else if ($(".Pat").hasClass("activeItem")) {
            $('.Joe .slayerItemPicture').removeClass('stickit-end');
      $('.Joe .slayerItemPicture').removeClass('stickit');
      topImageHeight = $('.Pat .slayerItemPicture').height();
    scrollBottom = topImage - $(window).height() + topImageHeight;
    windowScroll = $(window).scrollTop();
      bottomContainer = $("#CULTURE").offset().top - $(window).height();
    if(windowScroll > scrollBottom && windowScroll < bottomContainer){
        $('.Pat .slayerItemPicture').addClass('stickit');
      $('.Pat .slayerItemPicture').removeClass('stickit-end');
    }
      else if (windowScroll >= bottomContainer) {
      $('.Pat .slayerItemPicture').removeClass('stickit');
      $('.Pat .slayerItemPicture').addClass('stickit-end');
      }
      else {
      $('.Pat .slayerItemPicture').removeClass('stickit-end');
      $('.Pat .slayerItemPicture').removeClass('stickit');
    }
    }
    else {
        $('.Joe .slayerItemPicture').removeClass('stickit-end');
  $('.Joe .slayerItemPicture').removeClass('stickit');
  $('.Pat .slayerItemPicture').removeClass('stickit-end');
  $('.Pat .slayerItemPicture').removeClass('stickit');
    }

  });
});

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

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