Files
nexusphp/public/js/nexus.js

104 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-08-10 17:38:05 +08:00
jQuery(document).ready(function () {
jQuery('.spoiler-title').on('click', function () {
let content = jQuery(this).parent().next();
if (content.hasClass('collapse')) {
content.height(content[0].scrollHeight).removeClass('collapse')
} else {
content.height(0).addClass('collapse')
}
})
2022-09-01 02:47:17 +08:00
function getImgPosition(e, imgEle) {
2022-09-01 18:31:09 +08:00
// console.log(e, imgEle)
2022-08-23 02:25:51 +08:00
let imgWidth = imgEle.prop('naturalWidth')
let imgHeight = imgEle.prop("naturalHeight")
2022-08-31 23:08:26 +08:00
let ratio = imgWidth / imgHeight;
2022-09-01 02:47:17 +08:00
let offsetX = 10;
let offsetY = 10;
2022-09-01 18:31:09 +08:00
let width = window.innerWidth - e.clientX;
let height = window.innerHeight - e.clientY;
2022-09-01 12:47:31 +08:00
let changeOffsetY = 0;
2022-09-01 02:47:17 +08:00
let changeOffsetX = false;
2022-09-01 19:08:19 +08:00
if (e.clientX > window.innerWidth / 2 && e.clientX + imgWidth > window.innerWidth) {
2022-09-01 02:47:17 +08:00
changeOffsetX = true
2022-09-01 18:31:09 +08:00
width = e.clientX
2022-09-01 02:47:17 +08:00
}
2022-09-01 18:31:09 +08:00
if (e.clientY > window.innerHeight / 2) {
if (e.clientY + imgHeight/2 > window.innerHeight) {
2022-09-01 12:47:31 +08:00
changeOffsetY = 1
2022-09-01 18:31:09 +08:00
height = e.clientY
} else if (e.clientY + imgHeight > window.innerHeight) {
2022-09-01 12:47:31 +08:00
changeOffsetY = 2
2022-09-01 18:31:09 +08:00
height = e.clientY
2022-09-01 12:47:31 +08:00
}
2022-08-31 23:08:26 +08:00
}
2022-09-01 03:02:48 +08:00
let log = `innerWidth: ${window.innerWidth}, innerHeight: ${window.innerHeight}, pageX: ${e.pageX}, pageY: ${e.pageY}, imgWidth: ${imgWidth}, imgHeight: ${imgHeight}, width: ${width}, height: ${height}, offsetX: ${offsetX}, offsetY: ${offsetY}, changeOffsetX: ${changeOffsetX}, changeOffsetY: ${changeOffsetY}`
2022-09-01 02:47:17 +08:00
console.log(log)
if (imgWidth > width) {
imgWidth = width;
2022-09-01 03:02:48 +08:00
imgHeight = imgWidth / ratio;
2022-09-01 02:47:17 +08:00
}
if (imgHeight > height) {
imgHeight = height;
2022-08-31 23:08:26 +08:00
imgWidth = imgHeight * ratio;
}
2022-09-01 02:47:17 +08:00
if (changeOffsetX) {
2022-09-01 19:08:19 +08:00
offsetX = -(e.clientX - width + 10)
2022-09-01 02:47:17 +08:00
}
2022-09-01 12:47:31 +08:00
if (changeOffsetY == 1) {
2022-09-01 19:08:19 +08:00
offsetY = - (imgHeight - (window.innerHeight - e.clientY))
2022-09-01 12:47:31 +08:00
} else if (changeOffsetY == 2) {
offsetY = - imgHeight/2
2022-08-23 02:25:51 +08:00
}
2022-09-01 02:47:17 +08:00
return {imgWidth, imgHeight,offsetX, offsetY}
}
// preview
function getPosition(e, position) {
return {
left: e.pageX + position.offsetX,
top: e.pageY + position.offsetY,
width: position.imgWidth,
height: position.imgHeight
2022-08-23 02:25:51 +08:00
}
}
2022-08-10 17:38:05 +08:00
var previewEle = jQuery('#nexus-preview')
2022-09-01 02:47:17 +08:00
var imgEle, selector = 'img.preview', imgPosition
2022-08-24 13:36:14 +08:00
jQuery("body").on("mouseover", selector, function (e) {
2022-08-23 02:25:51 +08:00
imgEle = jQuery(this);
2022-09-01 02:47:17 +08:00
imgPosition = getImgPosition(e, imgEle)
let position = getPosition(e, imgPosition)
2022-08-23 02:25:51 +08:00
let src = imgEle.attr("src")
2022-08-10 17:38:05 +08:00
if (src) {
2022-08-23 02:25:51 +08:00
previewEle.attr("src", src).css(position).fadeIn("fast");
2022-08-10 17:38:05 +08:00
}
2022-08-24 13:36:14 +08:00
}).on("mouseout", selector, function (e) {
2022-08-21 15:22:08 +08:00
previewEle.fadeOut("fast");
2022-08-24 13:36:14 +08:00
}).on("mousemove", selector, function (e) {
2022-09-01 02:47:17 +08:00
let position = getPosition(e, imgPosition)
2022-08-23 02:25:51 +08:00
previewEle.css(position)
2022-08-21 15:22:08 +08:00
})
2022-08-24 13:36:14 +08:00
2022-08-31 23:08:26 +08:00
// lazy load
if ("IntersectionObserver" in window) {
const imgList = [...document.querySelectorAll('.nexus-lazy-load')]
var io = new IntersectionObserver((entries) =>{
entries.forEach(item => {
// isIntersecting是一个Boolean值判断目标元素当前是否可见
if (item.isIntersecting) {
item.target.src = item.target.dataset.src
item.target.classList.add('preview')
// 图片加载后即停止监听该元素
io.unobserve(item.target)
}
})
}, {
root: document.querySelector('body')
})
// observe遍历监听所有img节点
imgList.forEach(img => io.observe(img))
}
2022-08-10 17:38:05 +08:00
})