Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Scrolling 新鮮事
Search
chph
July 06, 2016
Technology
210
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
110
DevOpsDays Taipei 2017 敏捷思維分享
chph
0
130
在 Google Cloud Platform 架設你的網站伺服器並撰寫 Node.js 應用程式
chph
0
740
Intro to Progressive Web Apps
chph
1
82
淺談 Gzip
chph
0
150
Install WordPress on Linode
chph
0
180
Introduction Infrastructure - Linode 入門
chph
0
240
高速傳愛~三小時進化 PWA
chph
0
320
Optimize JavaScript execution and parse time using optimize-js
chph
0
170
Other Decks in Technology
See All in Technology
M5Stackで作る BLEマウス GPSログの世界
tomokusaba
0
100
宣言型部分更新は何を解決しどこへ向かうのか
shuta13
1
320
パッチ更新でterraform initが落ちた日
grimoh
1
120
AI時代のAPI開発を加速する品質ガードレール / API Quality Guardrails in the AI Era
yokawasa
0
190
Kiro Crewをはじめよう
kizawa2020
1
140
初対面のメンバーがチームを組み、Claude Codeでゼロからサービスをリリース
mkdev10
1
120
V8のArray.prototype.flatを最大約20倍速くするまでと、巨大OSSへの大規模コミットの道のり
riyaamemiya
0
270
⾮構造化データを価値に変える AI エージェント基盤とその組織
ivry_presentationmaterials
0
130
触りたくないデータに、どう触るか AI時代のRBACアクセスガバナンス
ivry_presentationmaterials
0
120
全人類(ほぼ)AWS Organizations の上でAWSを利用している、その世界を知る話
htan
1
140
LLM機能を自作して分かるSnowflake Cortex AIの強み
nayuts
0
370
ブラウザとサーバーから考えるAI時代のフロントエンド監視
shogo4131
3
860
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Exploring anti-patterns in Rails
aemeredith
4
530
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
980
RailsConf 2023
tenderlove
30
1.6k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
From π to Pie charts
rasagy
1
400
Deep Space Network (abreviated)
tonyrice
0
360
Discover your Explorer Soul
emna__ayadi
2
1.3k
Building AI with AI
inesmontani
PRO
1
1.3k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
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]