Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scrolling 新鮮事
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
chph
July 06, 2016
Technology
200
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Scrolling 新鮮事
前端魔法師七月小聚 @ 達暉資訊
chph
July 06, 2016
More Decks by chph
See All by chph
Chrome Dev Tools 基礎技巧
chph
0
100
DevOpsDays Taipei 2017 敏捷思維分享
chph
0
120
在 Google Cloud Platform 架設你的網站伺服器並撰寫 Node.js 應用程式
chph
0
720
Intro to Progressive Web Apps
chph
1
76
淺談 Gzip
chph
0
140
Install WordPress on Linode
chph
0
170
Introduction Infrastructure - Linode 入門
chph
0
230
高速傳愛~三小時進化 PWA
chph
0
300
Optimize JavaScript execution and parse time using optimize-js
chph
0
170
Other Decks in Technology
See All in Technology
AIをフル活用してオンコール機能のプロトタイプを2日で作った話 / Building an AI-Powered On-Call Prototype in Just Two Days
nari_ex
0
180
product engineering with qa
nealle
0
140
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
140
GuardrailからGovernanceへ~AIエージェント運用の次の課題~
sbspsy
1
150
5分でわかる Amazon Connect_20260608
hwangbyeonghun
0
180
Hatena Engineer Seminar 37 jj1uzh
jj1uzh
0
430
Keeping applications secure by evolving OAuth 2.0 and OpenID Connect
ahus1
PRO
1
140
知見・人・API・DB・予算 ─ ナイナイ尽くしだった人事データ整備 with dbt、5年間の学び
ken6377
1
150
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
220
Compose 新機能総まとめ / What's New in Jetpack Compose
yanzm
0
130
AI Agent SaaS を支える自社仮想化基盤への挑戦と実運用 / ai-agent-saas-virtualization
flatt_security
2
1.2k
完全自律ロボットを作りたくて、先に開発を自律させた話(ROS Japan UG #63 LT)
rryz09
0
230
Featured
See All Featured
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
Odyssey Design
rkendrick25
PRO
2
720
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
210
Speed Design
sergeychernyshev
33
1.9k
Code Review Best Practice
trishagee
74
20k
From π to Pie charts
rasagy
0
230
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
230
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
250
Music & Morning Musume
bryan
47
7.3k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.9k
Transcript
曾坤福 @afutseng Scrolling 新鮮事 前端魔法師∘2016.07.06
關於我 ! 曾坤福 (Afu) @afutseng Front-end Engineer at PIXNET Web
developer since 2006 ZCE (PHP 5.3) since Jan 2012 ZCPE (PHP 5.5) since Dec 2014
Improving scroll performance with passive event listeners https://www.youtube.com/watch?v=65VMej8n23A
問題 • 瀏覽器必須等待滾動/觸控的 handler 執⾏完畢才知道是否呼叫 preventDefault(),可能使捲動不流暢 (jank) • wheel, touchstart,
touchend, touchmove • empty touch event handler 也會影響 • Google 統計 Android 超過 80% 的 touch handlers 並不會取消 預設⾏為 • 也就是說通常瀏覽器都⽩等了 • 10% delay > 100ms • 1% > 500ms
解決⽅案 - {passive: true} • 表⽰該 handler 不會取消預設⾏為 (例如滾動) •
瀏覽器不⽤等待它執⾏完成才進⾏滾動 • 執⾏ preventDefault() 無效,瀏覽器會發出警告 • Chrome 51 & Firefox 49 開始⽀援
解決⽅案 - {passive: true} document.addEventListener(‘touchstart’, function (evt) { // Do
some thing here… // But don’t block scrolling ! }, {passive: true});
可是... 第三個參數傳 object 會讓⽼舊瀏覽器出錯耶?
addEventListener @ MDN /* 現代瀏覽器 */ target.addEventListener(type, listener[, options]); options
/* optional */ • capture = false 預設為事件冒泡 (Bubbling) • passive = false 承諾 listener 永不呼叫 preventDefault • once = false ⼀次性的 listener,觸發後⾃動移除 .PEFSO
addEventListener @ MDN /* Firefox 6、IE 9、Opera 11 */ target.addEventListener(type,
listener[,useCapture]); • useCapture = false 預設為事件冒泡 (Bubbling) • optional ,可不傳
addEventListener @ MDN /* IE 8 以下⽼舊瀏覽器 */ target.addEventListener(type, listener,
useCapture); • 必須傳第三個參數,通常是 false,表⽰作為冒泡階段的 handler
若是現代瀏覽器,傳⼊ {passive: true} ⽼舊瀏覽器則給 false
回顧跨瀏覽器處理原則 Feature Detection • (X) 根據瀏覽器/版本決定作法 • if (navigator.userAgentuser.match(/MSIE/)) {
…… } • (O) 瀏覽器⽀援某個功能,則採取特定作 法,反之做降級處理 (或是放⽣)
Feature Detection for {passive} var supportsPassive = false; try {
var opts = Object.defineProperty({}, 'passive', { get: function() { supportsPassive = true; } }); window.addEventListener("test", null, opts); } catch (e) { } IUUQTHJUIVCDPN8*$(&WFOU-JTUFOFS0QUJPOTCMPCHIQBHFTFYQMBJOFSNE
Feature Detection for {passive} ! elem.addEventListener('touchstart', myHandler, supportsPassive
? { passive: true } : false ); IUUQTHJUIVCDPN8*$(&WFOU-JTUFOFS0QUJPOTCMPCHIQBHFTFYQMBJOFSNE
⼩結 • 加上 {passive: true} 讓你的網⾴觸控體驗 更流暢 • 若成效不彰,使⽤ Chrome
的 Timeline 錄 製觸控/捲動事件找出效能瓶頸! !
Scroll Anchoring 捲動錨定 調整捲動位置,以免畫⾯外內容變更時造成檢視焦點位移
Scroll Anchoring OFF
啟⽤ Scroll Anchoring 實驗功能 1. 於 Chrome 網址列輸⼊ about:flags 2.
啟⽤捲軸錨定 ! 3. 重新啟動 Chrome
Scroll Anchoring ON
WJFXQPSU BODIPSOPEF -BZPVU SFqPX TDSPMMBEKVTUNFOU
References 1. Improving scroll performance with passive event listeners, https://developers.google.com/web/
updates/2016/06/passive-event-listeners 2. New APIs to help developers improve scroll performance, Chromium Blog, https:// blog.chromium.org/2016/05/new-apis-to-help-developers-improve.html 3. Passive Event Listeners, BlineOn 2016, https://docs.google.com/presentation/d/ 1lYmNxxNWeLHZBp8eWCSZY2RHIchGK-O9Ws4mhlAwFi0/edit#slide=id.p 4. Passive Event Listeners (EventListenerOptions), https://github.com/WICG/EventListenerOptions 5. EventTarget.addEventListener(), MDN, https://developer.mozilla.org/en-US/docs/Web/API/ EventTarget/addEventListener 6. 3.6. Interface EventTarget, DOM Living Standard, https://dom.spec.whatwg.org#interface- eventtarget 7. DOM Listener: capture, passive, and once, https://www.webreflection.co.uk/blog/2016/04/17/ new-dom4-standards 8. passive 的事件监听器, http://www.cnblogs.com/ziyunfei/p/5545439.html 9. 5.5 List of TouchEvent types, https://w3c.github.io/touch-events/#list-of-touchevent-types 10. Scroll Anchoring, BlinkOn 2016, https://docs.google.com/presentation/d/ 1bgVPqd805L38oAG7WbVr4jUP-YwOazjEGPNPLsIesBg/edit#slide=id.p
Photo Credits 1. https://www.flickr.com/photos/bracketing_life/4215944196 2. http://kaohsiung.network.com.tw/ 3. http://handmadecssdesign.com/blog/end-of-life-for-ie6/ 4. http://www.srido.org.tw/resource/20
感謝聆聽 @afutseng
痞客邦 PIXNET 徵才中! • 前端⼯程師 • PHP ⼯程師 • MIS
⼯程師 • UI 設計師 • UX Researcher 請聯絡
[email protected]