Skip to content

Commit

Permalink
v1.3, close #129
Browse files Browse the repository at this point in the history
  • Loading branch information
yscoder committed Nov 25, 2016
1 parent cece4e6 commit 71a2be0
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 19 deletions.
4 changes: 2 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hexo-theme-indigo
# https://github.com/yscoder/hexo-theme-indigo
version: 1.2.7
version: 1.3

#添加新菜单项遵循以下规则
# menu:
Expand Down Expand Up @@ -32,7 +32,7 @@ menu:
rss: /atom.xml

#你的头像url
avatar: //placekitten.com/200/200
avatar: /img/avatar.jpg
#favicon
favicon: /favicon.ico

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-theme-material-indigo",
"version": "1.2.7",
"version": "1.3.0",
"description": "Material Design theme for Hexo.",
"keywords": [
"hexo",
Expand Down
23 changes: 21 additions & 2 deletions scripts/plugins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
hexo.extend.tag.register('image', ([src, title]) => {
function renderImage(src, title) {
return `<figure class="image-bubble">
<img src="${src}" alt="${title}">
<div class="img-lightbox">
<div class="aspect-ratio">
<div class="placeholder"></div>
<img src="${src}" alt="${title}">
</div>
</div>
<div class="image-caption">${title}</div>
</figure>`
}


hexo.extend.tag.register('image', ([src, title]) => {
return renderImage(src, title)
})

hexo.extend.filter.register('before_post_render', data => {
data.content = data.content.replace(/\!\[(.*)\]\((.+)\)/gmi, (match, title, src) => {
const attrs = src.split(' ')
title = title || (attrs[1] && attrs[1].replace(/\"|\'/g, '')) || '未定义'
return `{% image ${attrs[0]} ${title} %}`
})
return data
})
15 changes: 6 additions & 9 deletions source/css/_partial/article.less
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
padding: 35px;
background: #fff;
border-radius: 4px;
box-shadow: 0 0 30px rgba(0, 0, 0, .3);
box-shadow: 0 10px 30px rgba(0, 0, 0, .2);
.post-meta {
margin-top: 8px;
}
Expand Down Expand Up @@ -206,13 +206,13 @@
}

.image-bubble {
img {
margin: 20px auto 10px;
}
margin-top: 20px;
margin-bottom: 20px;
text-align: center;
}
.image-caption {
display: inline-block;
margin-top: 10px;
color: @secondaryTextColor;
}

Expand Down Expand Up @@ -329,7 +329,8 @@
p:first-child,
pre:first-child,
table:first-child,
ul:first-child {
ul:first-child,
figure:first-child {
margin-top: 0
}

Expand All @@ -344,10 +345,6 @@
max-width: 100%;
}

img {
display: block;
margin: 40px auto;
}

blockquote {
position: relative;
Expand Down
59 changes: 59 additions & 0 deletions source/css/_partial/lightbox.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.img-lightbox {
text-align: center;
.aspect-ratio {
position: relative;
}

img {
display: inline;
margin: 0;
cursor: pointer;
cursor: -webkit-zoom-in;
&.zoom-img {
cursor: -webkit-zoom-out;
}
}

.zoom-img,
.zoom-img-wrap {
position: relative;
z-index: 889;
.transition(.3s)
}
.zoom-img-wrap.abs {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

.zoom-overlay {
position: fixed;
z-index: 888;
background: rgba(0, 0, 0, .6);
opacity: 0;
cursor: pointer;
cursor: -webkit-zoom-out;
.transition(.3s);
&.show {
opacity: 1;
+ .zoom-img-title {
opacity: 1;
}
}
}

.zoom-img-title {
position: fixed;
z-index: 900;
height: 30px;
line-height: 30px;
color: #fff;
background: rgba(0, 0, 0, .3);
opacity: 0;
.transition(.3s);
}

}

6 changes: 4 additions & 2 deletions source/css/_partial/postlist.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

.post-list-item {
padding: 0 12px;
margin-bottom: 24px;
margin-bottom: 30px;
}

.post-time {
Expand Down Expand Up @@ -94,8 +94,10 @@
padding: 0px 16px;
line-height: 28px;
color: rgba(255, 255, 255, 0.8);
.transition(.2s);
&:hover {
color: #fff
color: #fff;
box-shadow: 0 4px 8px rgba(0, 0, 0, .26);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/css/_partial/variable.less
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

&:hover,
&:active {
box-shadow: 0 5px 11px 0 rgba(0, 0, 0, 0.18), 0 4px 15px 0 rgba(0, 0, 0, 0.15);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 4px 15px rgba(0, 0, 0, 0.2);
}

}
Expand Down
2 changes: 2 additions & 0 deletions source/css/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ html {
color: @primaryTextColor;
font-size: @font-size;
line-height: 1.5;
overflow-x: hidden;
}

body {
Expand Down Expand Up @@ -399,4 +400,5 @@ hr,
@import '_partial/tags';
@import '_partial/search';
@import '_partial/reward';
@import '_partial/lightbox';
@import '_duoshuo/embed';
Binary file added source/img/avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 127 additions & 1 deletion source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,133 @@
}
}

})()
})(),
lightbox: function () {
var zoomedImg;
var screenSize = {};
var margin = 0;
var scrollbarWidth = w.innerWidth - docEl.offsetWidth;

function updateScreenSize() {
screenSize.x = w.innerWidth || docEl.clientWidth || body.clientWidth;
screenSize.y = w.innerHeight || docEl.clientHeight || body.clientHeight;
}
updateScreenSize();

function zoom() {
if (!this.isZoomed) {
this.isZoomed = !this.isZoomed;
zoomedImg = this;

if (!this.img)
this.img = this.querySelector('img');

var imgH = this.img.getBoundingClientRect().height;
var imgW = this.img.getBoundingClientRect().width;
var imgL = this.img.getBoundingClientRect().left;
var imgT = this.img.getBoundingClientRect().top;

var realW = this.img.naturalWidth || imgW,
realH = this.img.naturalHeight || imgH;

this.placeholder = this.querySelector('.placeholder');
this.placeholder.style.cssText = 'height: ' + imgH + 'px';

var top = (screenSize.y - imgH) / 2;
var left = (screenSize.x - this.offsetWidth) / 2;

this.img.classList.add('zoom-img');
this.overlay = d.createElement('div');
this.overlay.id = 'the-overlay';
this.overlay.className = 'zoom-overlay';
this.overlay.style.cssText = 'height:' + screenSize.y + 'px; width: ' + screenSize.x + 'px; top: -' + top + 'px; left: -' + left + 'px';

this.wrapper = d.createElement('div');
this.wrapper.id = 'the-wrapper';
this.wrapper.className = 'zoom-img-wrap abs';
this.wrapper.appendChild(this.img);
this.wrapper.appendChild(this.overlay);
this.children[0].appendChild(this.wrapper);

var title = this.img.title || this.img.alt;
if (title) {
this.caption = d.createElement('div');
this.caption.className = 'zoom-img-title';
this.caption.innerHTML = title;
this.caption.style.cssText = 'width: ' + screenSize.x + 'px; top: ' + (screenSize.y - top - 30) + 'px; left: -' + left + 'px';
this.wrapper.appendChild(this.caption);
}

var wrapX = ((screenSize.x - scrollbarWidth) / 2) - imgL - (imgW / 2);
var wrapY = imgT * (-1) + (screenSize.y - imgH) / 2;
var scale = 1;

if (realH > imgH) {
if (imgH === imgW && screenSize.y > screenSize.x) {
scale = screenSize.x / imgW;
} else if (imgH === imgW && screenSize.y < screenSize.x) {
scale = (screenSize.y - margin) / imgH;
} else if (imgH > imgW) {
scale = (screenSize.y - margin) / imgH;
if (scale * imgW > screenSize.x) {
scale = screenSize.x / imgW;
}
} else if (imgH < imgW) {
scale = screenSize.x / imgW;
if (scale * imgH > screenSize.y) {
scale = (screenSize.y - margin) / imgH;
}
}
}

if (scale * imgW > realW) {
scale = realW / imgW;
}

var that = this;
setTimeout(function () {
var wrapTrf = 'translate3d(' + wrapX + 'px, ' + wrapY + 'px, 0)';
var imgTrf = 'scale(' + scale + ')';

that.wrapper.style.cssText = 'transform: ' + wrapTrf + ';-webkit-transform: ' + wrapTrf;
that.img.style.cssText = 'transform: ' + imgTrf + ';-webkit-transform: ' + imgTrf;
that.overlay.className = 'zoom-overlay show';
}, 0);

} else {
this.isZoomed = !this.isZoomed;
zoomedImg = null
this.img.style.cssText = '';
this.wrapper.style.cssText = '';
this.overlay.className = 'zoom-overlay';

var that = this;
setTimeout(function () {
that.placeholder.style.cssText = '';
that.children[0].appendChild(that.img);
that.children[0].removeChild(that.wrapper);
that.img.classList.remove('zoom-img');
}, 300)
}
}

forEach.call($$('.post-content .img-lightbox'), function (el) {
el.addEventListener(even, zoom);
});

return {
updateScreenSize: updateScreenSize,
zoomOut: function () {
if (zoomedImg) {
zoomedImg[even]();
}
}
}
}
};

var lightbox = Blog.lightbox();

w.addEventListener('load', function () {
Blog.fixNavMinH();
Blog.waterfall();
Expand All @@ -288,6 +412,7 @@
Blog.fixNavMinH();
Blog.toggleMenu();
Blog.waterfall();
lightbox.updateScreenSize();
});

gotop.addEventListener(even, function () {
Expand Down Expand Up @@ -317,6 +442,7 @@
Blog.fixedHeader(top);
Blog.toc.fixed(top);
Blog.toc.actived(top);
lightbox.zoomOut();
}, false);

if (w.BLOG.SHARE) {
Expand Down
Loading

4 comments on commit 71a2be0

@xdcxdc
Copy link
Contributor

@xdcxdc xdcxdc commented on 71a2be0 Nov 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现的问题1 :
` 内的部分仍然会被渲染,如
image
渲染为了

<code>
    <div class="img-lightbox">
        <div class="aspect-ratio">
            <div class="placeholder"></div>
            
        <img src="http://i.xdc.at/assets/avatar/avatar.png" alt="图片替代文字" class=""></div>
    </div>
    <div class="image-caption">图片替代文字</div>
</code>

问题2:
当画面中只有图片的下半部分时,点击放大失效。

问题3:
浏览器窗口宽度较小,但足够显示图片原图大小时,图片仍会被轻度改变大小,如果图片内容是文字截图的话就会比较明显得看到。同时,页面最下方的标题文字也发虚了。这种情况对于宽度大于高度的图片更易出现。

问题4:
当浏览器窗口高度较小,对于高度大于宽度的图片,画面中只显示其上半部分时,点击放大,半透明遮罩不会出现、最下方的标题文字发虚。

功能建议:

  • 目前是把 alt 属性渲染成了图片下方标题,是否使用 title 属性更好?
  • 图片放大模式下,缩放浏览器窗口,布局动态跟随变化。
  • 图片放大模式 增强为 图片阅览模式,即在图片放大模式下可左右切换文章内的图片

@xdcxdc
Copy link
Contributor

@xdcxdc xdcxdc commented on 71a2be0 Nov 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能是 chrome 版本的问题,问题2在 57.0 上出现,54.0 上没问题。

@xdcxdc
Copy link
Contributor

@xdcxdc xdcxdc commented on 71a2be0 Nov 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

移动端,手指放在图片上滑动页面时,也会触发图片放大,体验不是很好。

@Wegome
Copy link

@Wegome Wegome commented on 71a2be0 Nov 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

移动端 firefox也有这样的问题

Please sign in to comment.