3

Below is the link of codePen, you can see if its window size then image get border on hover,

In mobile devices it get border on touch. but it doesn't go away if user is not touching it (after touching it). user needs to touch outside the image then its border goes away.

In the below image, user touch the image and its showing border, later user is not touching it and its still showing border. enter image description here

 .swap {
  background-image: url('https://farm9.staticflickr.com/8382/8558295631_0f56c1284f_b.jpg');
  width: 200px;
}

.swap a {
  display: block;
}

.swap a img {

  width: 200px;
  height: auto;


}
.swap a:hover img {
  border:10px black solid;
}

.swap a:focus img {
  border:none !important;
}
<div class="swap">
  <a>
    <img src="https://vignette.wikia.nocookie.net/undertale-au/images/5/54/Link.jpg/revision/latest?cb=20170903211129">
  </a>
</div>
Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
Developer
  • 279
  • 6
  • 22
  • https://codepen.io/SahilKatia/pen/GBGEXa – Developer Aug 03 '18 at 16:59
  • FYI, hover not working in mobile/touch device to handle hover event you need touch event using javascript or jQuery just like [https://stackoverflow.com/questions/4755505/how-to-recognize-touch-events-using-jquery-in-safari-for-ipad-is-it-possible] – Bharat Makvana Aug 03 '18 at 18:36
  • thanks, just now I found this out too, using touchstart and touchend together on one element will solve my problem – Developer Aug 03 '18 at 18:52

3 Answers3

1

Adding the :focus pseudo class will work for you to override what is happening.

.swap a:hover img {
  border:10px black solid;
}

.swap a:focus img {
  border:none !important;
}

If you are working on a responsive project that you do not want :focus to show on non-touch devices you can try to target devices by size, or more reliably, you could use Modernizr to feature detect.

brianespinosa
  • 2,408
  • 12
  • 22
1

So I solved this question by JavaScript events,ontouchstart and ontouchend please check out the below plunkr link

https://plnkr.co/edit/bVFQMUjJXo5SvLGroQH3?p=preview

function myFunction()
{
    document.getElementById('swap').setAttribute("class", "style1");
}

function myFunctions()
{
    document.getElementById('swap').setAttribute("class", "style2");
}



 <div id="swap">
  <a><img src="https://vignette.wikia.nocookie.net/undertale-au/images/5/54/Link.jpg/revision/latest?cb=20170903211129" ontouchstart="myFunction()" ontouchend="myFunctions()"   >
      </a>
    </div>
Developer
  • 279
  • 6
  • 22
0

Prepared for another query, just replace P with your element. It now seems best to avoid using hover altogether with ios or touch in general. The below code applies your css as long as touch is maintained, and without other ios flyouts. Do this;

  1. Jquery add: $("p").on("touchstart", function(e) { $(this).focus(); e.preventDefault(); });

  2. CSS: replace p:hover with p:focus, and add p:active

Options;

  • replace jquery p selector with any class etc

  • to have the effect remain, keep p:hover as well, and add body{cursor:ponter;} so a tap anywhere ends it

  • try click & mouseover events as well as touchstart in same code (but not tested)

  • remove e.preventDefault(); to enable users to utilise ios flyouts eg copy

Notes

  • only tested for text elements, ios may treat inputs etc differently

  • only tested on iphone XR ios 12.1.12, and ipad 3 ios 9.3.5, using Safari or Chrome.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
darren
  • 55
  • 1
  • 2