Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
L-Y.
driver.js
提交
8755ee26
D
driver.js
项目概览
L-Y.
/
driver.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
driver.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8755ee26
编写于
3月 04, 2018
作者:
K
Kamran Ahmed
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add docs and refactor
上级
3ee072c1
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
109 addition
and
38 deletion
+109
-38
assets/scripts/src/element.js
assets/scripts/src/element.js
+13
-3
assets/scripts/src/overlay.js
assets/scripts/src/overlay.js
+54
-13
assets/scripts/src/position.js
assets/scripts/src/position.js
+13
-4
assets/scripts/src/sholo.js
assets/scripts/src/sholo.js
+27
-2
index.html
index.html
+2
-16
未找到文件。
assets/scripts/src/element.js
浏览文件 @
8755ee26
import
Position
from
'
./position
'
;
import
Position
from
'
./position
'
;
export
default
class
Element
{
export
default
class
Element
{
/**
* DOM element object
* @param node
*/
constructor
(
node
)
{
constructor
(
node
)
{
this
.
element
=
node
;
this
.
element
=
node
;
this
.
document
=
document
;
this
.
document
=
document
;
}
}
// Gets the screen co-ordinates for the current dom element
/**
* Gets the screen co-ordinates (x,y) for the current dom element
* @returns {{x: number, y: number}}
*/
getScreenCoordinates
()
{
getScreenCoordinates
()
{
let
tempNode
=
this
.
element
;
let
tempNode
=
this
.
element
;
...
@@ -23,8 +30,11 @@ export default class Element {
...
@@ -23,8 +30,11 @@ export default class Element {
return
{
x
,
y
};
return
{
x
,
y
};
}
}
// Gets the calculated position on screen
/**
getPosition
()
{
* Gets the calculated position on screen, around which
* we need to draw
*/
getCalculatedPosition
()
{
const
coordinates
=
this
.
getScreenCoordinates
();
const
coordinates
=
this
.
getScreenCoordinates
();
const
position
=
new
Position
({
const
position
=
new
Position
({
left
:
Number
.
MAX_VALUE
,
left
:
Number
.
MAX_VALUE
,
...
...
assets/scripts/src/overlay.js
浏览文件 @
8755ee26
...
@@ -5,6 +5,11 @@ import Position from './position';
...
@@ -5,6 +5,11 @@ import Position from './position';
* cutting out the visible part, animating between the sections etc
* cutting out the visible part, animating between the sections etc
*/
*/
export
default
class
Overlay
{
export
default
class
Overlay
{
/**
* @param opacity number
* @param padding number
* @param animate bool
*/
constructor
({
constructor
({
opacity
=
0.75
,
opacity
=
0.75
,
padding
=
10
,
padding
=
10
,
...
@@ -29,7 +34,9 @@ export default class Overlay {
...
@@ -29,7 +34,9 @@ export default class Overlay {
this
.
setSize
();
this
.
setSize
();
}
}
// Prepares the overlay
/**
* Prepares the overlay
*/
prepareContext
()
{
prepareContext
()
{
const
overlay
=
this
.
document
.
createElement
(
'
canvas
'
);
const
overlay
=
this
.
document
.
createElement
(
'
canvas
'
);
...
@@ -44,14 +51,18 @@ export default class Overlay {
...
@@ -44,14 +51,18 @@ export default class Overlay {
this
.
overlay
.
style
.
zIndex
=
'
999999999
'
;
this
.
overlay
.
style
.
zIndex
=
'
999999999
'
;
}
}
// Highlights the dom element on the screen
/**
* Highlights the dom element on the screen
* @param element Element
* @param animate bool
*/
highlight
(
element
,
animate
=
true
)
{
highlight
(
element
,
animate
=
true
)
{
if
(
!
element
)
{
if
(
!
element
)
{
return
;
return
;
}
}
// get the position of element around which we need to draw
// get the position of element around which we need to draw
const
position
=
element
.
getPosition
();
const
position
=
element
.
get
Calculated
Position
();
if
(
!
position
.
canHighlight
())
{
if
(
!
position
.
canHighlight
())
{
return
;
return
;
}
}
...
@@ -68,6 +79,9 @@ export default class Overlay {
...
@@ -68,6 +79,9 @@ export default class Overlay {
this
.
draw
();
this
.
draw
();
}
}
/**
* Removes the overlay and cancel any listeners
*/
clear
()
{
clear
()
{
this
.
positionToHighlight
=
new
Position
();
this
.
positionToHighlight
=
new
Position
();
this
.
highlightedElement
=
null
;
this
.
highlightedElement
=
null
;
...
@@ -81,9 +95,9 @@ export default class Overlay {
...
@@ -81,9 +95,9 @@ export default class Overlay {
}
}
/**
/**
* `draw` is called for in requestAnimationFrame.
Here is what it does
* `draw` is called for in requestAnimationFrame.
Puts back the
*
- Puts back the filled overlay on body (i.e. while clearing existing highlight if any)
*
filled overlay on body (i.e. while removing existing highlight if any) and
*
- Slowly eases towards the item to be selected
*
Slowly eases towards the item to be selected.
*/
*/
draw
()
{
draw
()
{
// Cache the response of this for re-use below
// Cache the response of this for re-use below
...
@@ -99,7 +113,7 @@ export default class Overlay {
...
@@ -99,7 +113,7 @@ export default class Overlay {
const
isFadingIn
=
this
.
overlayAlpha
<
0.1
;
const
isFadingIn
=
this
.
overlayAlpha
<
0.1
;
if
(
isFadingIn
)
{
if
(
isFadingIn
)
{
// Ignore the animation, just highlight the item
// Ignore the animation, just highlight the item
at its current position
this
.
highlightedPosition
=
this
.
positionToHighlight
;
this
.
highlightedPosition
=
this
.
positionToHighlight
;
}
else
{
}
else
{
// Slowly move towards the position to highlight
// Slowly move towards the position to highlight
...
@@ -110,7 +124,7 @@ export default class Overlay {
...
@@ -110,7 +124,7 @@ export default class Overlay {
}
}
}
}
//
Remove the cloak from the position to highlight
//
Cut the chunk of overlay that is over the highlighted item
this
.
removeCloak
({
this
.
removeCloak
({
posX
:
this
.
highlightedPosition
.
left
-
window
.
scrollX
-
this
.
padding
,
posX
:
this
.
highlightedPosition
.
left
-
window
.
scrollX
-
this
.
padding
,
posY
:
this
.
highlightedPosition
.
top
-
window
.
scrollY
-
this
.
padding
,
posY
:
this
.
highlightedPosition
.
top
-
window
.
scrollY
-
this
.
padding
,
...
@@ -118,8 +132,8 @@ export default class Overlay {
...
@@ -118,8 +132,8 @@ export default class Overlay {
height
:
(
this
.
highlightedPosition
.
bottom
-
this
.
highlightedPosition
.
top
)
+
(
this
.
padding
*
2
),
height
:
(
this
.
highlightedPosition
.
bottom
-
this
.
highlightedPosition
.
top
)
+
(
this
.
padding
*
2
),
});
});
// Fade the overlay in if we can highlight
if
(
canHighlight
)
{
if
(
canHighlight
)
{
// Fade the overlay in if we can highlight
if
(
!
this
.
animate
)
{
if
(
!
this
.
animate
)
{
this
.
overlayAlpha
=
this
.
opacity
;
this
.
overlayAlpha
=
this
.
opacity
;
}
else
{
}
else
{
...
@@ -154,7 +168,16 @@ export default class Overlay {
...
@@ -154,7 +168,16 @@ export default class Overlay {
}
}
}
}
// Removes the patch of given width and height from the given position (x,y)
/**
* Removes the cloak from the given position
* i.e. cuts the chunk of layout which is over the element
* to be highlighted
*
* @param posX number
* @param posY number
* @param width number
* @param height number
*/
removeCloak
({
removeCloak
({
posX
=
0
,
posX
=
0
,
posY
=
0
,
posY
=
0
,
...
@@ -164,7 +187,15 @@ export default class Overlay {
...
@@ -164,7 +187,15 @@ export default class Overlay {
this
.
context
.
clearRect
(
posX
,
posY
,
width
,
height
);
this
.
context
.
clearRect
(
posX
,
posY
,
width
,
height
);
}
}
// Adds the cloak i.e. covers the given position with dark overlay
/**
* Adds the overlay i.e. to cover the given
* position with dark overlay
*
* @param posX number
* @param posY number
* @param width number
* @param height number
*/
addCloak
({
addCloak
({
posX
=
0
,
posX
=
0
,
posY
=
0
,
posY
=
0
,
...
@@ -175,6 +206,12 @@ export default class Overlay {
...
@@ -175,6 +206,12 @@ export default class Overlay {
this
.
context
.
fillRect
(
posX
,
posY
,
width
,
height
);
this
.
context
.
fillRect
(
posX
,
posY
,
width
,
height
);
}
}
/**
* Sets the size for the overlay
*
* @param width number
* @param height number
*/
setSize
(
width
=
null
,
height
=
null
)
{
setSize
(
width
=
null
,
height
=
null
)
{
// By default it is going to cover the whole page and then we will
// By default it is going to cover the whole page and then we will
// cut out a chunk for the element to be visible out of it
// cut out a chunk for the element to be visible out of it
...
@@ -182,8 +219,12 @@ export default class Overlay {
...
@@ -182,8 +219,12 @@ export default class Overlay {
this
.
overlay
.
height
=
height
||
this
.
window
.
innerHeight
;
this
.
overlay
.
height
=
height
||
this
.
window
.
innerHeight
;
}
}
// Refreshes the overlay i.e. sets the size according to current window size
/**
// And moves the highlight around if necessary
* Refreshes the overlay i.e. sets the size according to current window size
* And moves the highlight around if necessary
*
* @param animate bool
*/
refresh
(
animate
=
true
)
{
refresh
(
animate
=
true
)
{
this
.
setSize
();
this
.
setSize
();
...
...
assets/scripts/src/position.js
浏览文件 @
8755ee26
...
@@ -4,10 +4,10 @@
...
@@ -4,10 +4,10 @@
*/
*/
export
default
class
Position
{
export
default
class
Position
{
/**
/**
* @param left
* @param left
number
* @param top
* @param top
number
* @param right
* @param right
number
* @param bottom
* @param bottom
number
*/
*/
constructor
({
constructor
({
left
=
0
,
left
=
0
,
...
@@ -21,10 +21,19 @@ export default class Position {
...
@@ -21,10 +21,19 @@ export default class Position {
this
.
bottom
=
bottom
;
this
.
bottom
=
bottom
;
}
}
/**
* Checks if the position is valid to be highlighted
* @returns {boolean}
*/
canHighlight
()
{
canHighlight
()
{
return
this
.
left
<
this
.
right
&&
this
.
top
<
this
.
bottom
;
return
this
.
left
<
this
.
right
&&
this
.
top
<
this
.
bottom
;
}
}
/**
* Checks if the given position is equal to the passed position
* @param position Position
* @returns {boolean}
*/
equals
(
position
)
{
equals
(
position
)
{
return
this
.
left
.
toFixed
(
3
)
===
position
.
left
.
toFixed
(
3
)
&&
return
this
.
left
.
toFixed
(
3
)
===
position
.
left
.
toFixed
(
3
)
&&
this
.
right
.
toFixed
(
3
)
===
position
.
right
.
toFixed
(
3
)
&&
this
.
right
.
toFixed
(
3
)
===
position
.
right
.
toFixed
(
3
)
&&
...
...
assets/scripts/src/sholo.js
浏览文件 @
8755ee26
...
@@ -6,6 +6,11 @@ import './polyfill';
...
@@ -6,6 +6,11 @@ import './polyfill';
* Plugin class that drives the plugin
* Plugin class that drives the plugin
*/
*/
export
default
class
Sholo
{
export
default
class
Sholo
{
/**
* @param opacity number
* @param padding number
* @param animate boolean
*/
constructor
({
constructor
({
opacity
=
0.75
,
opacity
=
0.75
,
padding
=
10
,
padding
=
10
,
...
@@ -28,30 +33,50 @@ export default class Sholo {
...
@@ -28,30 +33,50 @@ export default class Sholo {
this
.
bind
();
this
.
bind
();
}
}
/**
* Binds any DOM events listeners
* @todo: add throttling in all the listeners
*/
bind
()
{
bind
()
{
// @todo: add throttling in all the listeners
this
.
document
.
addEventListener
(
'
scroll
'
,
this
.
onScroll
,
false
);
this
.
document
.
addEventListener
(
'
scroll
'
,
this
.
onScroll
,
false
);
this
.
document
.
addEventListener
(
'
DOMMouseScroll
'
,
this
.
onScroll
,
false
);
this
.
document
.
addEventListener
(
'
DOMMouseScroll
'
,
this
.
onScroll
,
false
);
this
.
window
.
addEventListener
(
'
resize
'
,
this
.
onResize
,
false
);
this
.
window
.
addEventListener
(
'
resize
'
,
this
.
onResize
,
false
);
this
.
window
.
addEventListener
(
'
keyup
'
,
this
.
onKeyUp
,
false
);
this
.
window
.
addEventListener
(
'
keyup
'
,
this
.
onKeyUp
,
false
);
}
}
/**
* Handler for the onScroll event on document
* Refreshes without animation on scroll to make sure
* that the highlighted part travels with the scroll
*/
onScroll
()
{
onScroll
()
{
// Refresh without animation on scroll
this
.
overlay
.
refresh
(
false
);
this
.
overlay
.
refresh
(
false
);
}
}
/**
* Handler for the onResize DOM event
* Refreshes with animation on scroll to make sure that
* the highlighted part travels with the width change of window
*/
onResize
()
{
onResize
()
{
// Refresh with animation
// Refresh with animation
this
.
overlay
.
refresh
(
true
);
this
.
overlay
.
refresh
(
true
);
}
}
/**
* Clears the overlay on escape key process
* @param event
*/
onKeyUp
(
event
)
{
onKeyUp
(
event
)
{
if
(
event
.
keyCode
===
27
)
{
if
(
event
.
keyCode
===
27
)
{
this
.
overlay
.
clear
();
this
.
overlay
.
clear
();
}
}
}
}
/**
* Highlights the given selector
* @param selector
*/
highlight
(
selector
)
{
highlight
(
selector
)
{
let
domElement
;
let
domElement
;
...
...
index.html
浏览文件 @
8755ee26
...
@@ -44,22 +44,8 @@
...
@@ -44,22 +44,8 @@
<script
src=
"./assets/scripts/dist/sholo.js"
></script>
<script
src=
"./assets/scripts/dist/sholo.js"
></script>
<script>
<script>
const
nodesToSelect
=
[
const
sholo
=
new
Sholo
();
'
.section__header
'
,
sholo
.
highlight
(
'
.section__header
'
);
'
.section__how
'
,
'
.section__examples
'
];
const
sholo
=
new
Sholo
({
padding
:
10
,
animate
:
true
});
nodesToSelect
.
forEach
((
nodeToSelect
,
index
)
=>
{
window
.
setTimeout
(()
=>
{
sholo
.
highlight
(
nodeToSelect
);
},
index
*
1000
);
});
</script>
</script>
</body>
</body>
</html>
</html>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录