Last active
November 25, 2016 10:23
-
-
Save Rplus/ba16bda29255002c17be3e634f458029 to your computer and use it in GitHub Desktop.
detection for the direction of selecting text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: http://stackoverflow.com/a/16265811 | |
// 用 createRange 創一新的選擇範圍來判斷圈選的方向性 | |
// anchorNode 是開頭圈選的 node, focusNode 是停止圈的位置 | |
// 當圈選起始點在後面,那這個生成的 range 會壓縮成一個疊合的位置,`range.toString()` 時會變成空字串 '' | |
// 不過我這邊調整了一下,直接輸出 rg.collapsed 比較原生,不用再轉一次 `toString()` | |
function isBackwards (sel) { | |
var rg = document.createRange(); | |
rg.setStart(sel.anchorNode, sel.anchorOffset); | |
rg.setEnd(sel.focusNode, sel.focusOffset); | |
return rg.collapsed; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via: https://www.facebook.com/groups/f2e.tw/permalink/1131939853510045/