Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

清理评论加载的代码 #586

Merged
merged 5 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion content/search.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
title: 搜索
date: '2009-03-15T23:48:26+00:00'
weight: 8
menu: main
---
Expand Down
34 changes: 3 additions & 31 deletions layouts/partials/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,11 @@
</section>

<div id="comment-box">
<iframe id="forum-frame" style="visibility:hidden;border:none;display:block;width:100%;height:0;"></iframe>
<iframe id="forum-frame"></iframe>
</div>

<script type="text/javascript" src="/js/comments.js"></script>
Copy link
Member

Choose a reason for hiding this comment

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

理论上此处应该用 {{ relURL "/js/comments.js" }} 以保证可移植性,不过在这个网站里问题不大。

Copy link
Member Author

Choose a reason for hiding this comment

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

我是从 search.js 那儿复制过来的。:joy:
统一改一下吧。

<script type="text/javascript">
function show_comment_box() {
var frame = document.getElementById("forum-frame");
frame.src = "{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}";
frame.style.height = "800px";
frame.style.visibility = "visible";
}

// https://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling/488073#488073
function is_visible(elem) {
var elem_top = elem.getBoundingClientRect().top;
var elem_bot = elem.getBoundingClientRect().bottom;
var is_vis = (elem_top >= 0) && (elem_bot <= window.innerHeight);
return is_vis;
}

var comment_box = document.getElementById("comment-box");
// If the comment box is already in the current screen, start loading comments
if(is_visible(comment_box)) {
show_comment_box()
} else {
// Otherwise, attach the action to the scroll event
document.body.onscroll = function() {
var comment_box = document.getElementById("comment-box");
if(is_visible(comment_box)) {
// Run only once
document.body.onscroll = null;
show_comment_box();
}
}
}
comments_delayed_loading("comment-box", "forum-frame", "{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}");
Copy link
Member

Choose a reason for hiding this comment

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

这个做法仍然暴露了一个全局对象 comments_delayed_loading。我的建议是把 iframe 真正的 src 属性临时存储在 data-src 属性中,然后你的 comments.js 脚本就可以实现零参数运行了。把 data-src 属性读出来赋给 src 属性,iframe 就加载了。

我之所以想让你用这个办法,是因为这个办法可以很容易移植给 #579 用(只不过应用给 <img> 对象而已),一箭双雕。

Copy link
Member Author

Choose a reason for hiding this comment

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

明白。

Copy link
Member Author

Choose a reason for hiding this comment

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

但 ID 是不是就得写死进函数里了?

Copy link
Member

@yihui yihui Jul 14, 2017

Choose a reason for hiding this comment

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

不,ID 写在外面:<iframe id="forum-frame" data-src="{{ .Site.Params.comment_embed_url }}{{ .Params.forum_id }}"></iframe>,函数里面只做一件事,就是把 data-src 替换为 src。

Copy link
Member Author

Choose a reason for hiding this comment

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

哦,我是说要找到这个 <iframe> 就得知道它的 ID forum-frame,这个是不是得写进匿名函数里?还是说默认对所有 <iframe> 都这么来一遍?但即使是这样也得知道外面那个盒子的 ID。

Copy link
Member

@yihui yihui Jul 14, 2017

Choose a reason for hiding this comment

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

处理所有带 data-src 属性的 iframe:document.getElementsByTagName('iframe')

</script>
{{ end }}
10 changes: 10 additions & 0 deletions static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ a { color: #f05948; }
}
.post-nav a, .pagination a { text-decoration: none; }
.active a { text-decoration: underline; }

/* iframe of Flarum page */
#forum-frame {
display: block;
border: none;
width: 100%;
/* height and visibility will be changed by Javascript on loading */
visibility: hidden;
height: 0;
}
34 changes: 34 additions & 0 deletions static/js/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function comments_delayed_loading(box_id, iframe_id, iframe_url) {

function show_comment_box() {
var frame = document.getElementById(iframe_id);
frame.src = iframe_url;
frame.style.height = "800px";
Copy link
Member

Choose a reason for hiding this comment

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

800有没有略大?这个我也没有什么意见。

frame.style.visibility = "visible";
}

// https://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling/488073#488073
function is_visible(elem) {
var elem_top = elem.getBoundingClientRect().top;
var elem_bot = elem.getBoundingClientRect().bottom;
var is_vis = (elem_top >= 0) && (elem_bot <= window.innerHeight);
return is_vis;
}

var comment_box = document.getElementById(box_id);
// If the comment box is already in the current screen, start loading comments
if(is_visible(comment_box)) {
show_comment_box();
} else {
// Otherwise, attach the action to the scroll event
document.body.onscroll = function() {
var comment_box = document.getElementById(box_id);
if(is_visible(comment_box)) {
// Run only once
document.body.onscroll = null;
show_comment_box();
}
}
}

}