- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2020 04:00 PM
Our team has a requirement to develop a floating button (much like the virtual chat) and we've done so after following this pretty simple HTML/CSS tutorial. The button looks great and functions correctly, except that when a user scrolls all the way to the bottom of the page, the bottom appears in the footer. Ideally we would want the floating button to be position fixed 40px from the right and 40px from the bottom until it reaches the footer. Once a user scrolls to the footer, the button should not overlap with the footer, but instead stay 40px above the footer. We found some solutions on stackoverflow, but it would require jquery and I don't believe there's a way to use jquery on page level CSS. Are there any ideas on how we can accomplish this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2020 05:48 AM
Hi
Yeah, sorry about that, scrollTopMax is a firefox (Mozilla) thing. It lets you know the maximum scroll an element can do.
I modified the script to this:
function link(scope, element, attrs, controller) {
//element that is actually scrolling
$('section.sp-scroll').bind('scroll',function(evt){
//footer element
var footerEl = $('#footer-con');
//if the current scroll position is greater than its calculated value of its scrollHeight minus its height minus the footers height
if(this.scrollTop > (this.scrollHeight - this.offsetHeight - footerEl[0].scrollHeight)){
var footerHeight = footerEl.height();
var floatBottom = $('.float');
//set position of the button with an additional 40% (based off it's current 40px)
$('.float').css({"bottom": footerHeight + (footerHeight * .4) });
}else{
$('.float').css({"bottom":"40px"});
}
});
}
I really didn't expect you to use my script as I stated it's a rough mock and could be improved on so that it's not so choppy. I just wanted to show that it's possible to manipulate other elements even if their styles are set in some other place.
I can't really answer your question about what's going on with $(window) vs $(document) height being the same. But I can say this, when listening to the scroll event you'll want to listen to the actual element that is scrolling.
As you can see in my example I'm binding to a section element and not window or document because both of their scrollTop would continue to be zero. In this case those elements aren't scrolling.
.scrollTop, .scrollHeight, .offsetHeight are all attributes of an html element.
Hopefully that helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2020 08:30 PM
Hi,
I tried with the below. Please check if it works for you.
HTML
<div class="footer-floating-area">
<button class="floating-button">My button</button>
</div>
CSS
.footer-floating-area {
right: 0px;
top: 80%;
position: fixed;
}
.floating-button {
background-color: orange;
}
Result:
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2020 08:07 AM
Thanks for this, our code looks similar to this, however our footer is about 470px high so having top:80%, the button will still overlap our footer.