refactor zoomImage
This commit is contained in:
parent
fd0d5b70be
commit
087b1d9a61
|
@ -4,7 +4,6 @@ export function directive($timeout) {
|
|||
let idContainer = 'zoomImage';
|
||||
let container;
|
||||
let background;
|
||||
let image;
|
||||
|
||||
function createContainers(src) {
|
||||
if (document.getElementById(idContainer)) {
|
||||
|
@ -15,31 +14,21 @@ export function directive($timeout) {
|
|||
|
||||
background = document.createElement('div');
|
||||
background.className = 'zoomImage-background';
|
||||
background.style.backgroundImage = `url(${src})`;
|
||||
container.appendChild(background);
|
||||
|
||||
image = document.createElement('img');
|
||||
image.src = src;
|
||||
container.appendChild(image);
|
||||
|
||||
document.body.appendChild(container);
|
||||
|
||||
$timeout(() => {
|
||||
resizeImage();
|
||||
container.className = 'open';
|
||||
}, 250);
|
||||
}
|
||||
|
||||
function addListeners() {
|
||||
background.addEventListener('click', destroyContainers);
|
||||
document.addEventListener('keydown', e => keyDownHandler(e));
|
||||
window.addEventListener('resize', resizeImage);
|
||||
}
|
||||
|
||||
function removeListeners() {
|
||||
if (container) {
|
||||
background.removeEventListener('click', destroyContainers);
|
||||
document.removeEventListener('keydown', e => keyDownHandler(e));
|
||||
window.removeEventListener('resize', resizeImage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,13 +49,6 @@ export function directive($timeout) {
|
|||
image = undefined;
|
||||
}
|
||||
|
||||
function resizeImage() {
|
||||
if (image) {
|
||||
image.style.marginLeft = `-${Math.floor(image.clientWidth / 2)}px`;
|
||||
image.style.marginTop = `-${Math.floor(image.clientHeight / 2)}px`;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
restrict: 'A',
|
||||
priority: 9999,
|
||||
|
|
|
@ -9,32 +9,11 @@ div#zoomImage, div#zoomImage .zoomImage-background {
|
|||
top: 0;
|
||||
z-index: 11;
|
||||
}
|
||||
div#zoomImage{
|
||||
opacity: 0;
|
||||
transition: visibility 0s, opacity 0.5s linear;
|
||||
}
|
||||
|
||||
div#zoomImage .zoomImage-background{
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
background-color: rgba(0, 0, 0, 0.7);
|
||||
cursor: zoom-out;
|
||||
}
|
||||
div#zoomImage img{
|
||||
z-index: 12;
|
||||
position: fixed;
|
||||
max-height: 98%;
|
||||
max-width: 98%;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
opacity: 0;
|
||||
transition: visibility 0s, opacity 1s linear;
|
||||
}
|
||||
|
||||
div#zoomImage.open {
|
||||
visibility: visible;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
div#zoomImage.open img{
|
||||
visibility: visible;
|
||||
opacity: 1;
|
||||
background-repeat: no-repeat;
|
||||
background-position: center center;
|
||||
background-size: auto 98%;
|
||||
}
|
Loading…
Reference in New Issue