V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
iugo
V2EX  ›  JavaScript

通过 .classList.add('') 增加的 class (在移动端 WebKit)无法通过 .classList.remove('') 移除是为什么? 增加一个 class 的本质是什么?

  •  
  •   iugo · 2015-04-27 11:35:39 +08:00 · 3061 次点击
    这是一个创建于 3289 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个简单的例子:
    http://codepen.io/iugo/pen/pJveKZ

    一个实用的例子:
    http://codepen.io/iugo/pen/aOzJRe

    简单的例子在 Firefox 和移动端都很好地实现了功能.

    但是实用的例子在 Firefox 实现了功能, 但在移动端却无法移除 class, 导致功能缺失.

    测试环境:
    Firefox: Ubuntu(64bit) 37
    移动端: iOS 8(Safari) and Android 4.4(Chrome)
    9 条回复    2015-04-27 15:38:13 +08:00
    iugo
        1
    iugo  
    OP
       2015-04-27 12:03:59 +08:00
    简单的例子在 Android 4.4(Chrome) 上正常.
    但是在 iOS 8(Safari) 上不正常.
    morethansean
        2
    morethansean  
       2015-04-27 12:15:34 +08:00
    iugo
        3
    iugo  
    OP
       2015-04-27 14:29:14 +08:00
    @morethansean 应该不是这么简单的问题.

    如果需要移除 html 中写的 class, 则没有问题.
    但如果使用一个函数向 classList 中增加 class, 则 Firefox 正常, WebKit 无法实现.
    otakustay
        4
    otakustay  
       2015-04-27 14:39:34 +08:00   ❤️ 1
    iOS safari就没触发scroll事件,不是classList的问题……
    iugo
        6
    iugo  
    OP
       2015-04-27 14:44:27 +08:00
    @otakustay 移动端由 scroll 类似的事件吗? 或有什么库推荐吗?
    iugo
        7
    iugo  
    OP
       2015-04-27 14:48:29 +08:00
    @otakustay 简单的例子和实用的例子在 Chrome(Android) 里也是有区别的.

    在简单的例子中:
    只要滑动就触发了事件绑定的函数.

    在实用的例子中:
    滑动后需要在空白处点击一下才触发.

    这个是什么情况?
    otakustay
        8
    otakustay  
       2015-04-27 14:48:38 +08:00   ❤️ 1
    @iugo Stackoverflow上有些相关的问题

    http://stackoverflow.com/questions/9225525/how-can-i-monitor-scroll-position-while-scrolling-in-safari-on-ios

    总得来说乖乖touchstart的时候开始setInterval判断scrollTop……
    otakustay
        9
    otakustay  
       2015-04-27 15:38:13 +08:00   ❤️ 1
    @iugo 厄,我没有android的手机……我看有时间的话去借一个来帮你看看问题- -
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5470 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:54 · PVG 15:54 · LAX 00:54 · JFK 03:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.