I am trying to create a notification which will pop up, then disappear after a few seconds, but I want the user to be able to hover over it with the mouse to keep it from disappearing. If I could I'd like to also have a click make it disappear, but that's not necessary. I'm not sure how I could have the hover in html interact with the embedded ruby to stop the timeout. I'm aware I might need to restructure the whole thing to make it work. Here's the relevant css and html/erb snippets (not enough to run):
setTimeout(function() {
$('#alert_box').fadeOut('fast');
}, 3000);
.alert_wrap {
padding: 0px 50px;
margin-top: 3px;
height: 5%;
background-color: rgba(255, 0, 0, .3);
border: 3px solid red;
grid-row-start: 2;
justify-self: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<% unless notice == nil %>
<div id="alert_box" class="alert_wrap">
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
</div>
<% end %>
In the first step you can use
setTimeoutand then onmouseoverorhoverevent, you can clear the timeout function usingclearTimeoutand thus thefadeoutwill not take effect.And on
mouseoutyou can again use thesetTimeoutto start counting for 3 seconds.Also since you have mentioned about the click event, I have added a close button that on click can call the timeout function straight away.