Rails HTML 錨點 Anchor 怎麼用?外加如何微調錨點降落地點

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
1.14K   0  
·
2017/05/31
·
1 mins read


要讓使用者好用,錨點,也就是HTML 中的 Anchor 是少不了的。

設定錨點,就是給一個 division ID:

<div id="blog_id-<%= blog.id %>" style="padding-top: 4em;margin-top: -4em;"></div>

那後面這東東是什麼? 這就是調錨點降落地點的好物,各位可以改改看看,我的這個例子大概是讓網頁往下移4行,寫成inline因為可以省一點CSS,我覺得也不要太反對inline CSS,畢竟,一個正常上線的網站,都是上千行的CSS,像這樣只是為了Anchor位置上下調,又要多加好幾行CSS,再說,我覺得微調可以重複使用的機率不大。

Link_to 寫法如下:

link_to user_path(anchor: "blog_id-"+ blog.id.to_s)

Redirect_to 如下:

redirect_to blog_path(id: @blog.id, anchor: "blog_id-"+@blog.id.to_s) 

簡單好用就是美。

當使用 Turbolinks 5 時,就還有一點要注意的,link <a> 會讓 Turbolink 重新整頁網頁 reload,這很討厭,所以就要加上 data-turbolinks="false",把 Turbolinks 關了:

link_to "abc", blogs_path(id: blog, anchor: "blog_id-"+pc_blog.id.to_s), data: { turbolinks: false }

或是:

link_to "abc", "#blog_id-"+pc_blog.id.to_s, data: { turbolinks: false }

後面就會轉譯為:

<a data-turbolinks="false" href="#blog_id-305">abc</a>

這就是把 Turbolinks 關了,網頁內跳來跳去,就不會每次網頁 refresh 了。


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

Categories:
Tags:
Date:
Published: 2017/05/31 - Updated: 2017/12/21
Total: 283 words


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.