2

I am building a chat widget, which is minimized (collapsed and fixed to bottom of page) by default and then maximized when clicked.

It has a fixed height, and overflow-y: scroll. I want the scrollbar to start at the bottom and scroll upwards, but this is prettry problematic.

If I never collapse the widget, which I do with widget.toggle('blind') (JQuery), I can simply scroll with javascript on page load: using .scrollTop(), however, I want the widget to initially be collapsed. Using .scrollTop() on the collapsed widget has no effect! Furthermore, whenever I collapse/expand the widget, it scrolls all the way to the top.

Is there a library or do you have some hints to solve this?

4
  • This seems related: stackoverflow.com/questions/14426780/… Commented Dec 9, 2015 at 14:24
  • indeed.. this is exactly it. But it doesn't work, when the widget is collapsed (overflowing box is hidden, i.e. height=0). So I'm looking for an alternative or something I've overlooked Commented Dec 9, 2015 at 14:28
  • 1
    Could you tie a JS event into collapsing/expanding the widget so it can scroll to bottom? Commented Dec 9, 2015 at 14:29
  • yea @PaulRedmond, I attached it as a callback widget.toggle('blind', function(){/* scroll here */}). Thanks for the hint Commented Dec 10, 2015 at 7:54

1 Answer 1

3

You can do it like this:

  1. Declare your chatView (widget) minimized height (5vh in my example) by default
  2. When user wants to open the chatView (click in my example), you adding class (open in my example) and increase it's height (90vh in my example). With transition property - you get wanted animation.
  3. Use mentioned jQuery method .scrollTop with needed container height (#chatView>div in my example), which insures it scroll to the bottom.

$(function(){
	$("#chatView").click(function(){
            $(this).toggleClass("open").scrollTop($("#chatView>div").height());
        });
});

		
		*{
			margin:0;
		}
		footer{
			height:10vh;
			position: absolute;
			bottom: 0;
		}
		#chatView{
			width: 20vw;
			background:red;
			position:absolute;
			bottom:0;
			height:4vh;
			transition: height 1s;
			overflow-y:scroll;
		}
		#chatView.open{
			height: 90vh;
		}
		#chatView>div{
			background:green;
			height: 95vh;
		}
		#chatView>figure{
			height: 4vh;
		}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<footer>
  <div id="chatView" >
    <figure></figure>
    <div ></div>
  </div>
</footer>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.