Wednesday 16 November 2011

Android Phonegap :active CSS pseudo class doesn't work. A workaround..

If you're building a cross-platform Phonegap app, you may well run in to the issue that your CSS :active pseudo-classes simply don't work when running on Android. They may work on your a (link) elements, but not on div or others.

Luckily there's a simple workaround. This workaround will continue using the fast, native, JavaScript-free :active class on iOS, and drops back to using the JavaScript on Android.

  • First, in your CSS, find every instance of the :active class. Add another rule that maps a class called fake-active to the same style.
E.g.

.my-button:active {
 background-color: blue;
}

Becomes:

my-button:active, .my-button.fake-active {
 background-color: blue;
}
  • Now, in your document ready event, when running on Android only, attach event handlers for touchstart and touchend to all the classes that have used :active. Make these add and remove the fake-active class.
if (navigator.userAgent.toLowerCase().indexOf("android") > -1) {
    $(".my-button")
    .bind("touchstart", function () {
        $(this).addClass("fake-active");
    })
   .bind("touchend", function() {
        $(this).removeClass("fake-active");
    });
    .bind("touchcancel", function() {
        // sometimes Android fires a touchcancel event rather than a touchend. Handle this too.
        $(this).removeClass("fake-active");
    });
}

Note that you can use the $(".my-button, .my-other-element, .even-more-elements") jQuery syntax to bind the events to other classes too.

21 comments:

  1. Thanks for this article - but FWIW it doesn't appear to be working for me in Android as I get the "stuck" issue even w/your touchcancel update.

    My app touch events all happen on touchstart - is that possibly why Android is never removing the "fake-active" class? I've used your script verbatim btw.

    ReplyDelete
  2. .bind("touchend", function() {
    $(this).removeClass("fake-active");
    }); <---- That semicolon sucks ;)

    ReplyDelete
  3. Tech Gadgets reviews and latest Tech and Gadgets news updates, trends, explore the facts, research, and analysis covering the digital world.
    You will see Some Tech reviews below,

    lg bluetooth headset : You will also wish to keep design and assorted features in mind. The most essential part of the design here is the buttonsof lg bluetooth headset .

    Fastest Car in the World : is a lot more than the usual number. Nevertheless, non-enthusiasts and fans alike can’t resist the impulse to brag or estimate according to specifications. Fastest Car in the World click here to know more.

    samsung galaxy gear : Samsung will undoubtedly put a great deal of time and even more cash into courting developers It is looking for partners and will allow developers to try out
    different sensors and software. It is preparing two variants as they launched last year. samsung galaxy gear is very use full click to know more.

    samsung fridge : Samsung plans to supply family-oriented applications like health care programs and digital picture frames along with games It should stick with what they know and they
    do not know how to produce a quality refrigerator that is worth what we paid. samsung fridge is very usefull and nice product. clickcamera best for travel: Nikon D850: Camera It may be costly, but if you’re trying to find the very best camera you can purchase at this time, then Nikon’s gorgeous DX50 DSLR will
    probably mark each box. The packaging is in a vibrant 45.4-megapixel full-frame detector, the picture quality is simply wonderful. However, this is just half the story. Because of a complex 153-point AF system along with a brst rate of 9 frames per minute. camera best specification. click here to know more.

    visit https://techgadgets.expert/ this site to know more.

    ReplyDelete
  4. How to stop smoking weed ???

    1.Do you want to know How to stop smoking weed or have you been wondering of how to get your dear one to stop smoking weed ?
    Every weed smoker knows deep down that quitting the behavior is an uphill task. And some people don`t know How to quit smoking weed .. They should
    Know How to quit smoking weed by visiting in this https://irvined.co.uk/ website.

    2.Long-term marijuana users may find the withdrawal experience uncomfortable. Marijuana detox helps one to slowly ease off of THC
    until it is completely eliminated from the body system. Marijuana detox also helps to reduce withdrawal symptoms thus making it
    easier for even highly addicted individuals to make full about turn in their weed smoking habits (avoiding relapse).

    3.The decision to stop smoking weed is usually impulsive for many individuals. If you have smoked pot long enough, you probably have plenty of memories
    where you did something and swore to yourself to stop your weed smoking habit going forward. And if you don`t know How to stop smoking pot ...
    Then visit https://irvined.co.uk/ here to know more.

    4.Quitting marijuana will give you the chance to become more responsible and set you in the right direction to progress in your life.
    And the good thing is that you don’t have to try quitting on your own now. ‘ Quit Marijuana The Complete Pack ’ is just a click away at a very affordable price.
    See more details about the guide and current purchase offers at https://quit-weed.com/. You can do this. Regardless of how long you have smoked pot or used marijuana
    in other ways, the quit marijuana pack offers you robust support to ensure that you achieve your goals. To know more information visit https://irvined.co.uk/ here.

    ReplyDelete
  5. Genyatra provides train tkts, flight tkts, senior citizen yatra services to its Clients across World.
    Ticketing
    All types of Domestic and International Flight ticket booking at competitive price. We provide best corporate fare and group fare across world.
    Packages
    We create specialized travel packages like family holidays, honeymoons, meetings, pilgrimage tours, special packages for senior citizen tours & women tours.
    Hotel Booking
    Get cheapest budget and luxury best deals on hotels in India and International location. our motto is to help you save money on accommodation across world.
    Visa and Forex
    We Specialize in visa assistance for Individual and Group. We provides foreign currency at best exchange available rates.we povide Travel insurance.
    Flight tkt, teerthyatra, foreign exchange rail tkt

    ReplyDelete
  6. Big Truck Tow: Heavy Duty towing service san jose

    We're rated the most reliable heavy duty towing san jose service & roadside assistance in San Jose!
    Call us now! We're ready to help you NOW!

    Since 1999, tow truck san jose has provided quality services to clients by providing them
    with the professional care they deserve. We are a professional and affordable Commercial
    Towing Company. BIG TRUCK TOW provides a variety of services, look below for the list of
    services we offer. Get in touch today to learn more about our heavy duty towing


    Click here to Find tow truck near me

    ReplyDelete
  7. Talk with Strangerstalk to strangers in Online Free Chat rooms where during a safe environment.
    From friendships to relationships.omegle teen Talk With Stranger is that the best online chatting site.
    Its the simplest alternative to airg chat, Badoo , omegle & mocospace. If you're keen on speaking
    with people on the web ,chat random or want to seek out omegle girls, do free texting or sexting, make new friends.
    you'll find your omegle lady here. Please note this is often not a sexting site so you can't do sexting
    online. this is often a familychatous friendly chat site. we've voice chat if you would like to try to to phone
    chat online. Our most viral is that the 1-1 one on one random chat.talkwithstranger No check in on login needed.
    we've teengers also asanonymous chat older people that want to satisfy new people. Online random chat is that the best
    chatrandom alternative.

    ReplyDelete

  8. ظهور نخستین سایت های شرط بندی
    اما نخستین سایت های شرط بندی چگونه شکل گرفتند؟ قبل از اینکه به مبحث اصلی مقاله یعنی معرفی سایت بازی انفجار نیترو بپردازیم، بد نیست کمی با ظهور نخستین سایت های شرط بندی آشنا شویم. اگر فراموش نکرده باشید اشاره کردیم بازی های شرط بندی در گذشته محدودیت های زیادی را داشتند.
    حضرات نیترو
    حضرات
    بازی رولت

    اما امروزه قدرت اینرنت باعث شده تا این بازی به سایت های شرط بندی کوچ کنند و از محدودیت مکانی خارج شوند. تاریخ دقیق ظهور سایت های شرط بندی در دسترس نمی باشد.
    بتخته نرد آنلاین نیترو
    تخته نرد آنلاین
    بلک جک نیترو

    اما با رجوع به تاریخچه بعضی از این سایت های از جمله سایت نیترو، خواهیم دریافت که پیشرفت تکنولوژی، بزرگترین ترغیب کننده برای ایجاد این سایت ها بوده است. البته مطالب گفته شده تمامی اطلاعات درباره این سایت های نمی باشد.
    پوکر آنلاین نیترو
    پوکر آنلاین
    بلک جک

    شمار سایت های شرط بندی امروزه رو به افزایش می باشد. برای مثال می توانیم به سایت نیترو اشاره کنیم. این سایت معتبر یکی از پیشگامان در این عرصه می باشد که قصد داریم در این مقاله شما را بیشتر با آن آشنا کنیم.
    پاسور آنلاین نیترو
    پاسور آنلاین

    ترفند برد و آموزش بازی انفجار آنلاین و شرطی، نیترو بهترین و پرمخاطب ‌ترین سایت انفجار ایرانی، نحوه برد و واقعیت ربات ها و ...
    Visit https://www.wmsociety.org/
    here for more information

    ReplyDelete
  9. I respect this article for the very much investigated substance and magnificent wording. I got so included in this material that I couldn't quit perusing. I am awed with your work and aptitude. Much obliged to you to such an extent.is ez battery reconditioning a scam

    ReplyDelete
  10. Would love to always get updated outstanding blog !undergroundfatloss4u

    ReplyDelete
  11. The way you write, you are really a professional blogger. https://unlockhipflexorsinfo.com/

    ReplyDelete
  12. This is such an awesome asset, to the point that you are giving and you give it away for nothing.our article has piqued a lot of positive interest. yoga burn dvd

    ReplyDelete
  13. This is such an awesome asset, to the point that you are giving and you give it away for nothing.our article has piqued a lot of positive interest. Tina Reviews

    ReplyDelete
  14. I just had to show my appreciation for the writer's point of view on this subject by leaving a comment. Thank you for writing such top notch content for your readers. It's not often I have the opportunity to enjoy great content like yours. Thank you for writing compelling and interesting material for readers like me. You have a great talent. chat us

    ReplyDelete
  15. One of the most popular tools for creating mobile apps is the Phonegap framework. In fact, developers have used Phonegap to create an estimated 4.5 million apps already! What does this mean for you? If you want to create a mobile app, then why use a Mobile App Developmentframework like Phonegap

    ReplyDelete
  16. Personally, I feel you've contributed a lot to this topic. Your article contains great quality content not often seen. I enjoyed reading this article very much. It can't be easy to make every paragraph interesting, but you managed it. for more in formation visit here:chat us

    ReplyDelete
  17. I can't recall the last time I enjoyed an informative article like I have yours. You must have higher standards than I do. You've exceeded my high standards and expectations with your excellent writing skills. Good article! I see your points as being valid and I appreciate the way you expressed yourself in this material. You must have higher standards than I do. chatous login

    ReplyDelete
  18. We know you are pretty excited about mobile technology, so we are going to talk today about one of the most important part of mobile app development, which is the ‘mobile application programming interface’ (API).An API is a collection of code, documentation and tools that help you build web and mobile apps. In addition to being able to store information about your users, web and mobile app development benefits from a database make it easy to add and edit information. This gives you the power to create a unique experience for each user and to make changes to your site quickly and easily.

    ReplyDelete
  19. I respect this article for the very much investigated substance and magnificent wording. I got so included in this material that I couldn't quit perusing. I am awed with your work and aptitude. Much obliged to you to such an extent. the just reviews

    ReplyDelete
  20. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks. https://thejustreviews.com

    ReplyDelete