The behavior you are raising is too dependent on the implementation of Materialize . And it seems that they did not develop taking into account that scenario ( Add an action to the closing event or provide the possibility of passing a callback to a closing event ).
It is true that the returnToOriginal function could be used, but there is no decent way to get access to it, let alone modify it.
The simplest way to achieve what you're looking for, feels a bit Hacky , but it can be simulated by passing a width with a very small value ( But not too much ). For example:
HTML:
<img id="img1" class="materialboxed" src="images/img1.jpg" width="0.2">
<a href="#!" id="linkImg1" onclick="document.getElementById('img1').click();">Imagen 1</a>
In this way the element can not be seen on the screen, the link works to awaken the effect, and Materialize continues to manage its closure.
Another way to achieve the same thing, feels much more Hacky and is much more complex. But it would be using javascript mainly. Something quick could be:
First, keep a reference to the currently open image ( When I click on a link ). Saved in OpenActually in this example.
Second, by having reference to the active image, I can know which one to close / hide. I do not overwrite the Materialize action, but let the animation complete ( At 200ms currently ), hide the element and destroy the reference since the image would not be currently active.
let elementoAbiertoActualmente;
function abrirElemento(selector) {
elementoAbiertoActualmente = document.getElementById(selector);
elementoAbiertoActualmente.classList.remove('hide');
elementoAbiertoActualmente.click();
}
function cerrarElemento() {
if (elementoAbiertoActualmente) {
let aCerrar = elementoAbiertoActualmente;
setTimeout(function() {
aCerrar.classList.add('hide');
}, 200);
elementoAbiertoActualmente = null;
}
}
Having the basic logic, we must prepare the HTML elements for this solution:
<img id="img1" class="hide materialboxed" src="images/img1.jpg" width="1">
<a href="#!" id="linkImg1" onclick="abrirElemento('img1');">Imagen 1</a>
And finally, handle the same events that you described for the closing; click, scroll and escape key. Something minimally functional:
$(document).keyup(function(e) {
if (e.keyCode === 27) {
cerrarElemento();
}
});
$(window).scroll(function() {
if (ultimoElemento) {
cerrarElemento();
}
});
$(document).click(function(e) {
if (ultimoElemento && e.target.tagName == "DIV") {
cerrarElemento();
}
});
With this you can already perform the same behavior. In this situation, the smaller the width that the img element has, the more pleasing it will be to the view.
But, another way to achieve the same, is the most obvious; abandon Materialize for this particular case, and manually implement that effect by css and js, already having full control of it.