I have encountered an issue while trying to showcase a floating bubble with a message located on the left side of the bubble. Despite using absolute positioning and setting a fixed value for the left
property, whenever the message text changes, the width of the div fluctuates causing it to move away from the bubble.
Could someone guide me on how I can position it in such a way that the floating message remains aligned to the left of the bubble no matter the length of the text?
Below is the code snippet:
var floatingMessage = document.querySelector("#floating-message");
var toggleBtn = document.querySelector("#toggleMessage");
var messages = ["Hello There 👋, chat with us!", "Hello There 👋", "Welcome!", "blah blah", "looooooooooooooooooooonoooooooooooooog text"];
var currentIndex = 0;
toggleBtn.addEventListener('click', function() {
currentIndex = (currentIndex + 1) % messages.length;
floatingMessage.textContent = messages[currentIndex];
});
body {
font-family: calibri;
background-color: lightgray;
}
#floating-bubble {
background-color: teal;
position: fixed;
bottom: 0;
right: 0;
margin-bottom: 20px;
margin-right: 20px;
border-radius: 50%;
width: 50px;
height: 50px;
box-shadow: 0 0 20px rgba(0,0,0,.5);
transition: box-shadow .5s ease-in-out;
cursor: pointer;
z-index: 2147483645;
border-bottom-right-radius: 0;
background-image: url('https://cdn0.iconfinder.com/data/icons/thin-communication-messaging/57/thin-036_bubble_comment_chat_message-512.png');
background-position: center;
background-size: 80%;
background-repeat: no-repeat;
border-bottom-right-radius: 0;
}
#floating-message {
background: #3a96dd;
padding: 5px;
left: -180px;
transition: none;
animation-duration: .5s;
animation-name: slidein,wiggle;
animation-iteration-count: 1,4;
animation-direction: normal,alternate;
animation-timing-function: ease-in,ease-in-out;
border-radius: 10px;
top: 10px;
font-size: 14px;
box-shadow: 0 0 10px #000;
position: absolute;
color: #fff;
font-family: Calibri, sans-serif;
}
<button id="toggleMessage">
Toggle Message
</button>
<div id="floating-bubble">
<div id="floating-message">Hello There 👋, chat with us!</div>
</div>