diff --git a/css/toast.css b/css/toast.css index 15093ae..6c3ae29 100644 --- a/css/toast.css +++ b/css/toast.css @@ -3,25 +3,25 @@ * @description Toast - A Bootstrap 4.2+ jQuery plugin for the toast component * @version 0.7.1 **/ -#toast-container { +.toast-container { position: sticky; z-index: 1055; top: 0 } -#toast-wrapper { +.toast-wrapper { position: absolute; top: 0; right: 0; margin: 5px } -#toast-container>#toast-wrapper>.toast { +.toast-container>.toast-wrapper>.toast { min-width: 150px; background-color: rgb(255, 255, 255); border-top: none; } -#toast-container>#toast-wrapper>.toast>.toast-header strong { +.toast-container>.toast-wrapper>.toast>.toast-header strong { padding-right: 20px; -} \ No newline at end of file +} diff --git a/js/toast.js b/js/toast.js index 8227222..74575fd 100644 --- a/js/toast.js +++ b/js/toast.js @@ -4,18 +4,25 @@ * @version 0.7.1 **/ (function ($) { - var TOAST_CONTAINER_HTML = '
'; - var TOAST_WRAPPER_HTML = ''; + var TOAST_CONTAINER_HTML = ''; + var TOAST_WRAPPER_HTML = ''; $.toast = function (opts) { - if (!$('#toast-container').length) { - $('body').prepend(TOAST_CONTAINER_HTML); - $('#toast-container').append(TOAST_WRAPPER_HTML); + // If container is not set in opts use body + var general_container = $('body'); + if (opts.container && opts.container.length === 1) + general_container = opts.container; - $('body').on('hidden.bs.toast', '.toast', function () { + // if toast container and wrapper are not present in container create them + if (!general_container.children('.toast-container').length) { + general_container.prepend(TOAST_CONTAINER_HTML); + general_container.children('.toast-container').append(TOAST_WRAPPER_HTML); + + general_container.on('hidden.bs.toast', '.toast', function () { $(this).remove(); }); } + var toast_wrapper = general_container.children('.toast-container').children('.toast-wrapper'); var id = 'toast-' + ($('.toast').length + 1), html = '', @@ -100,8 +107,8 @@ html += ''; - $('#toast-wrapper').append(html); - $('#toast-wrapper .toast:last').toast('show'); + toast_wrapper.append(html); + toast_wrapper.find('.toast:last').toast('show'); if (pause_on_hover !== false) { setTimeout(function () { @@ -126,4 +133,4 @@ }); } } -}(jQuery)); \ No newline at end of file +}(jQuery));