-
Notifications
You must be signed in to change notification settings - Fork 243
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
清理评论加载的代码 #586
Changes from 2 commits
4bb4335
f954707
c61d00f
8f674a4
b8bc144
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
--- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
<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 }}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个做法仍然暴露了一个全局对象 我之所以想让你用这个办法,是因为这个办法可以很容易移植给 #579 用(只不过应用给 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 明白。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 但 ID 是不是就得写死进函数里了? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不,ID 写在外面: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 哦,我是说要找到这个 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 处理所有带 |
||
</script> | ||
{{ end }} |
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
理论上此处应该用
{{ relURL "/js/comments.js" }}
以保证可移植性,不过在这个网站里问题不大。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我是从
search.js
那儿复制过来的。:joy:统一改一下吧。