Skip to content

Commit f049dbb

Browse files
committed
Implement animated change transitions
1 parent 7b5335f commit f049dbb

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ Alternatively, instead of loading livereload.js from the LiveReload server, you
120120
<script src="https://github.com/livereload/livereload-js/raw/master/dist/livereload.js?host=localhost"></script>
121121
```
122122

123+
### Animated transitions
124+
125+
LiveReload can animate changes. To enable animated transitions pass `animate=true` to `livereload.js` when including in the script tag. The default transition duration is 280ms and is configurable via `animation_duration`. The value is integer in milliseconds.
126+
123127

124128
Issues & Limitations
125129
--------------------

src/livereload.coffee

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,19 @@ exports.LiveReload = class LiveReload
8282
originalPath: message.originalPath || ''
8383
overrideURL: message.overrideURL || ''
8484
serverURL: "http://#{@options.host}:#{@options.port}"
85+
@performTransition() if !!(/true|1$/).test(LiveReload.options.animate)
8586

8687
performAlert: (message) ->
8788
alert message.message
8889

90+
performTransition: ->
91+
html = document.body.parentNode
92+
reloadedClass = ' livereload-reloaded '
93+
existingHtmlClass = html.getAttribute('class') ? ''
94+
html.setAttribute('class', "#{existingHtmlClass.replace(reloadedClass, '')} #{reloadedClass}")
95+
setTimeout (-> html.setAttribute('class', existingHtmlClass.replace(reloadedClass, ''))),
96+
parseInt(@options.animation_duration, 10) + 20
97+
8998
shutDown: ->
9099
@connector.disconnect()
91100
@log "LiveReload disconnected."
@@ -143,3 +152,19 @@ exports.LiveReload = class LiveReload
143152

144153
@connector.sendCommand { command: 'info', plugins: pluginsData, url: @window.location.href }
145154
return
155+
156+
setUpCSSTransitions: ->
157+
prefixer = (declaration) ->
158+
(['-webkit-', '-moz-', ''].map (item) -> ("#{item}#{declaration}")).join(' ')
159+
160+
head = document.getElementsByTagName('head')[0]
161+
styleNode = document.createElement("style")
162+
cssText = ".livereload-reloaded * { #{prefixer('transition: all ' +
163+
@options.animation_duration + 'ms ease-out;')} }"
164+
165+
if styleNode.styleSheet
166+
styleNode.styleSheet.cssText = cssText
167+
else
168+
styleNode.appendChild(document.createTextNode(cssText))
169+
170+
head.appendChild(styleNode)

src/options.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ exports.Options = class Options
1111
@maxdelay = 60000
1212
@handshake_timeout = 5000
1313

14+
@animate = false
15+
@animation_duration = 280 # ms
16+
1417
set: (name, value) ->
1518
if typeof value is 'undefined'
1619
return

src/startup.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LiveReload.addPlugin require('./less')
88

99
LiveReload.on 'shutdown', -> delete window.LiveReload
1010
LiveReload.on 'connect', ->
11+
LiveReload.setUpCSSTransitions() if !!(/true|1$/).test(LiveReload.options.animate)
1112
CustomEvents.fire document, 'LiveReloadConnect'
1213
LiveReload.on 'disconnect', ->
1314
CustomEvents.fire document, 'LiveReloadDisconnect'

0 commit comments

Comments
 (0)