1

I have a very simple web page. Here is my Demo.

Desktop: When I hover over the circle, it expands.

iPad: When I click the circle, it expands.

However, is it possible (on iPad) that when the circle is in the "expanded" state, if I click it again, it contracts?

How would I do this using my existing code?

Javascript:

$('.circle').on('touchstart',function(){});

Many thanks for any help with this.

2 Answers 2

2
var events = "mouseover mouseout";
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
    events = "touchstart";
};

$('.circle').on(events,function(){
   if ($(this).hasClass("expanded")) {
       // contract the circle
       $(this).addClass("contracted").removeClass("expanded");
   } else {
       // expand the circle
       $(this).addClass("expanded").removeClass("contracted");
   };
});

Now add class contracted to all your circles.

Sign up to request clarification or add additional context in comments.

4 Comments

Here is my amended code: jsbin.com/adacot/13/edit Any ideas why it doesn't "contract" if I click it when open?
Thanks for this. What would you recommend I change this to?
Thanks sir. I'm fairly new to Javascript as you can possibly tell. Would you be able to put together an example using my jsbin code?
Yep, I'm not a big fan of jsbin either, but jsfiddle was down for me earlier :'( I've noticed the 'hover' is now disabled on Desktop. Is there a way to fix that?
1

I would try using jQuery toggle: http://api.jquery.com/toggle-event/

$('.circle').toggle(function() {
  $(this).addClass("expanded").removeClass("contracted");
}, function() {
  $(this).removeClass("expanded").addClass("contracted");
});

1 Comment

Thanks for the reply, Vytautas. Can you show me using my code how I would implement this? Should I change my HTML?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.