Skip to content

Commit

Permalink
pref: 优化图片效果
Browse files Browse the repository at this point in the history
  • Loading branch information
AirboZH committed Mar 13, 2024
1 parent 4d24cc7 commit 9836de0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/css/addon/commons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,7 @@ main {
p {
> img[data-src],
> a.popup {
&:not(.normal):not(.left):not(.right) {
@include align-center;
}
display: inline-block;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
@import "bootstrap/dist/css/bootstrap.css";
@import "@fortawesome/fontawesome-free/css/all.css";
@import "tocbot/dist/tocbot.css";
@import "magnific-popup/dist/magnific-popup.css";
3 changes: 3 additions & 0 deletions src/js/modules/components/img-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*
* See: https://github.com/dimsemenov/Magnific-Popup
*/
import {preImg} from "../../patch/pre-img";

export function imgPopup() {
preImg();

if ($('.popup') <= 0) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/js/patch/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {codeClass, codeBlock} from './codeClass';
import {addClassToTable} from "./table";
import {activeSidebar} from "./sidebar";
import {preImg} from "./pre-img";

export const patch = {
codeClass: codeClass,
codeBlock: codeBlock,
addTableClass: addClassToTable,
activeSidebar: activeSidebar
activeSidebar: activeSidebar,
preImg: preImg,
};
25 changes: 25 additions & 0 deletions src/js/patch/pre-img.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function preImg() {
const imgSelector = '.content p img';
const images = document.querySelectorAll(imgSelector);
images.forEach((img) => {
let anchorElement = document.createElement('a')
let divElement = document.createElement('div')
anchorElement.setAttribute('href', img.getAttribute('src'));
anchorElement.classList.add('popup');
anchorElement.classList.add('img-link')
setWidth(img, anchorElement)

img.parentNode.insertBefore(divElement, img);
divElement.appendChild(anchorElement);
anchorElement.appendChild(img);
})
}

const setWidth = (img, anchorElement) => {
if (img.getAttribute('width'))
anchorElement.style.width = img.getAttribute('width')
if (img.getAttribute('height'))
anchorElement.style.height = img.getAttribute('height')
img.removeAttribute('width')
img.removeAttribute('height')
}
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {initTopbar} from "./js/modules/layouts/topbar";
import {categoryCollapse} from "./js/modules/components/category-collapse";
import {imgLazy} from "./js/modules/components/img-lazyload";
import {toc} from "./js/modules/components/toc";
import {initClipboard} from "./js/modules/plugins";
import {imgPopup, initClipboard} from "./js/modules/plugins";
// 因为 patch/index 中有内容且没有引用, 所有没有在 index 中引入
import { patch } from "./js/patch/";

Expand All @@ -26,6 +26,6 @@ patch.addTableClass();
patch.activeSidebar();
categoryCollapse();
imgLazy();
// imgPopup();
imgPopup();
initClipboard();
toc();

0 comments on commit 9836de0

Please sign in to comment.