Skip to content

Commit 2d57bbf

Browse files
committed
JavaScript: make image zoom always full width
1 parent d1d952e commit 2d57bbf

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

css/components/elements/_media.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ img.mag_popup {
6666
}
6767

6868
.mag_popup_container {
69+
background: var(--content-background);
6970
width:100%;
7071
position:absolute;
7172
z-index:1001;

js/pretext_add_on.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,33 @@ window.addEventListener("load",function(event) {
122122
/* click an image to magnify */
123123
$('body').on('click','.image-box > img:not(.draw_on_me):not(.mag_popup), .sbspanel > img:not(.draw_on_me):not(.mag_popup), figure > img:not(.draw_on_me):not(.mag_popup), figure > div > img:not(.draw_on_me):not(.mag_popup)', function(){
124124
var img_big = document.createElement('div');
125-
img_big.setAttribute('style', 'background:#fff;');
125+
const content_element = document.getElementById('ptx-content');
126126
img_big.setAttribute('class', 'mag_popup_container');
127-
img_big.innerHTML = '<img src="' + $(this).attr("src") + '" style="width:100%" class="mag_popup"/>';
127+
img_big.innerHTML = `<img src="${$(this).attr("src")}" style="width:100%;" class="mag_popup"/>`;
128128
// place_to_put_big_img = $(this).parents(".sbsrow, figure, li").last();
129129
place_to_put_big_img = $(this).parents(".image-box, .sbsrow, figure, li, .cols2 article:nth-of-type(2n)").last();
130130
// for .cols2, the even ones have to go inside the previous odd one
131131
if (place_to_put_big_img.prop("tagName") == "ARTICLE") {
132132
place_to_put_big_img = place_to_put_big_img.prev().children().first();
133133
}
134+
135+
// find ancestor so that place_to_put_big_img's position is relative to that ancestor
136+
var img_big_parent = place_to_put_big_img[0].parentElement;
137+
while (img_big_parent.id !== "ptx-content") {
138+
const computed_position = getComputedStyle(img_big_parent).position;
139+
if (computed_position !== "static") {
140+
break;
141+
}
142+
img_big_parent = img_big_parent.parentElement;
143+
}
144+
145+
const content_element_computed_style = getComputedStyle(content_element);
146+
const content_padding_left = parseFloat(content_element_computed_style.paddingLeft );
147+
const content_padding_right = parseFloat(content_element_computed_style.paddingRight);
148+
const img_big_offset = content_element.getBoundingClientRect().left - img_big_parent.getBoundingClientRect().left + content_padding_left;
149+
const doc_width = content_element.offsetWidth - content_padding_left - content_padding_right;
150+
img_big.setAttribute('style', `width:${doc_width.toString()}px; left:${img_big_offset.toString()}px;`);
151+
134152
$(img_big).insertBefore(place_to_put_big_img);
135153
});
136154

0 commit comments

Comments
 (0)