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
chph
July 06, 2016
Technology
190
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
160
Other Decks in Technology
See All in Technology
2026TECHFRESH畢業分享會 - Lightning Talk - 打造精準高效的 MCP 設計模式與測試實務
line_developers_tw
PRO
0
710
【Cyber-sec+】経営層を"動かす"ための考え方
hssh2_bin
0
120
MCP Appsを作ってみよう
iwamot
PRO
4
480
NAB Show 2026 動画技術関連レポート / NAB Show 2026 Report
cyberagentdevelopers
PRO
0
160
ポケモンの型をTypeScriptの型システムで表現してみた
subroh0508
0
370
AI駆動開発を通して感じた、 AI時代のデザイナーの役割変化
whisaiyo
0
190
Claude Code×Terraform IaC テンプレート駆動開発
itouhi
1
490
スキルと MCP ツール、責務をどう分けるか? AI が迷わないインターフェース設計の戦略
cdataj
1
920
現地で盛り上がった WWDC26 Keynote
zozotech
PRO
1
170
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
130
新しいVibe Codingと”自走”について
watany
5
290
EventBridge Connection
_kensh
5
690
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Making Projects Easy
brettharned
120
6.7k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
320
4 Signs Your Business is Dying
shpigford
187
22k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.5k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
600
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
ラッコキーワード サービス紹介資料
rakko
1
3.6M
HDC tutorial
michielstock
2
700
Agile that works and the tools we love
rasmusluckow
331
21k
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]