{"id":235,"date":"2014-01-21T13:29:22","date_gmt":"2014-01-21T13:29:22","guid":{"rendered":"http:\/\/freshinbox.com\/blog\/?p=235"},"modified":"2015-04-10T09:48:15","modified_gmt":"2015-04-10T09:48:15","slug":"interactive-images-on-mobile-email","status":"publish","type":"post","link":"https:\/\/freshinbox.com\/blog\/interactive-images-on-mobile-email\/","title":{"rendered":"Interactive Images on Mobile Email"},"content":{"rendered":"<p>In <a title=\"3 Steps to Create an Interactive Email Using CSS\" href=\"https:\/\/freshinbox.com\/blog\/3-steps-to-create-an-interactive-email-using-css\/\" target=\"_blank\">3 Steps to Create an Interactive Email Using CSS<\/a> I showed how to create &#8220;rollover images&#8221; in email wherein the user could move their cursor over an image and an alternate image will be shown. This tutorial will show you how to create a similar effect on mobile.<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/freshinbox.com\/blog\/wp-content\/uploads\/2014\/01\/switch-top.jpg\" \/><\/p>\n<p>A major difference between mobile and desktop\/web email clients is that there is no concept of a hover in mobile since there is no cursor. For this example we&#8217;ll add a space where the user can tap to display an alternative image.<\/p>\n<p>This example works on mobile email clients that <a title=\"Email Client Media Query and Embedded Styles Support 2014\" href=\"https:\/\/freshinbox.com\/blog\/email-client-media-query-and-embedded-styles-support-2014\/\">support media queries<\/a> such as the iPhone and Android&#8217;s native email clients which account for the majority of mobile email opens according to <a title=\"Email Client Market Share: Where People Opened in 2013\" href=\"https:\/\/litmus.com\/blog\/email-client-market-share-where-people-opened-in-2013\" target=\"_blank\">Litmus<\/a>.<\/p>\n<div id=\"email-me\">\n  <input type=email id=\"email-me-email\" placeholder=\"Email address\"> <input id=\"email-me-btn\" type=button value=\"Email Me This Example\"><BR>\n<\/div>\n<p><BR><\/p>\n<h4>Step 1: Front as Main Image and Back as Background<\/h4>\n<p>First we&#8217;re going to create an image element showing the primary image together with a link and wrap it in a div with the alternative image set as the background image.<\/p>\n<pre class=\"lang:default decode:true\">&lt;div class=\"mobi-swap-content\" style=\"background-image:url(http:\/\/...\/back-image.jpg);\"&gt;\r\n&lt;a href=\"http:\/\/..\/link\"&gt;&lt;img src=\"http:\/\/...\/front-image.jpg\" \r\n  style=\"display:block;\" height=\"265\" width=\"250\" alt=\"...\" border=\"0\"&gt;&lt;\/a&gt;\r\n&lt;\/div&gt;<\/pre>\n<h4>Step 2: Tap Triggers<\/h4>\n<p>Second, we&#8217;ll add the trigger elements that the user will tap on when viewed on a mobile device. Notice that this element is initially hidden so that recipients on non-mobile clients do not see them. An article explaining techniques to hide elements is <a title=\"Bulletproof Solution to Hiding Mobile Content when opened in Non-Mobile Email Clients\" href=\"https:\/\/freshinbox.com\/blog\/bulletproof-solution-to-hiding-mobile-content-when-opened-in-non-mobile-email-clients\/\">here<\/a>. All of it is wrapped within a div with the position set to relative. This allows us to position the triggers using absolute coordinates in the media query later.<\/p>\n<pre class=\"lang:default mark:2-7 decode:true\">&lt;div style=\"position:relative;\"&gt;\r\n  &lt;!--[if !mso 9]&gt;&lt;!--&gt;\r\n  &lt;div class=\"mobi-swap-close\" style=\"display:none;max-height:0px;overflow:hidden;\"&gt;\r\n  &lt;a href=\"#\"&gt;Tap here to show front&lt;\/a&gt;&lt;\/div&gt;\r\n  &lt;div class=\"mobi-swap-open\" style=\"display:none;max-height:0px;overflow:hidden;\"&gt;\r\n  &lt;a href=\"#\"&gt;Tap here to show back&lt;\/a&gt;&lt;\/div&gt;\r\n  &lt;!--&lt;![endif]--&gt;\r\n    &lt;div class=\"mobi-swap-content\" style=\"background-image:url(http:\/\/...\/back-image.jpg);\"&gt;\r\n    &lt;a href=\"http:\/\/..\/link\"&gt;&lt;img src=\"http:\/\/...\/front-image.jpg\" \r\n      style=\"display:block;\" height=\"265\" width=\"250\" alt=\"...\" border=\"0\"&gt;&lt;\/a&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<h4>Step 3: Mobile Media Queries<\/h4>\n<p>Third,\u00a0 we&#8217;ll add the CSS rules in a media query block that will display the trigger elements when the email is opened on a mobile device. Notice we&#8217;re using a modified way to write the CSS rule in order to <a title=\"How Yahoo! Mail's Mangling of Media Queries affects your CSS\" href=\"https:\/\/freshinbox.com\/blog\/how-yahoo-mails-mangling-of-media-queries-affects-your-css\/\">work around Yahoo!\u2019s Media Query issues<\/a> (Update: Yahoo! <a href=\"https:\/\/freshinbox.com\/blog\/yahoo-mail-fixes-media-query-bug-yahoo\/\">fixed this<\/a> in 2015):<\/p>\n<pre class=\"lang:default decode:true\">@media screen and (max-device-width: 480px) {\r\n  div[class=mobi-swap-open], div[class=mobi-swap-close]{\r\n    display: block !important;\r\n    max-height: none !important;   \r\n    background-color: #eeeeee;\r\n    padding-left:10px;\r\n    font: 15px Helvetica;\r\n    line-height:40px;\r\n  }\r\n  \/* Make the link fill up the space of its container *\/\r\n  div[class=mobi-swap-open] a, div[class=mobi-swap-close] a{\r\n    display:block;\r\n    text-decoration:none;\r\n  }\r\n  \/* Position the open trigger above the close trigger *\/\r\n  div[class=mobi-swap-open] {\r\n    position:absolute;\r\n    top:0px;\r\n    left:0px;\r\n    right:0px;\r\n  }\r\n}<\/pre>\n<h4>Step 4: :hover to Switch<\/h4>\n<p>Finally we&#8217;ll add the CSS rules to the media query block that will switch the image when the user taps on the open trigger:<\/p>\n<pre class=\"lang:default decode:true\">  div[class=mobi-swap-open]:hover {\r\n    visibility: hidden;\r\n  }\r\n  div[class=mobi-swap-open]:hover + div[class=mobi-swap-content] img{\r\n    visibility:hidden;\r\n  }<\/pre>\n<p>Here we are using the hover pseudoclass. Even though mobile environments do not support a cursor and hence you can\u2019t technically &#8220;hover&#8221; over an element, mobile environments such as the iPhone and Android trigger a hover event when a user taps on an element that is configured to respond to it. So in this case, the open trigger becomes hidden and exposes the close trigger underneath it.<\/p>\n<p>It is also worth pointing out that there is a &#8220;+&#8221; in the selector for the second rule. This is called a sibling selector. With a sibling selector,\u00a0when the left selector is active the CSS rules are applied to the elements that match the selector on the right (provided the right element is immediately after the element in the left selector)*<\/p>\n<p>In this case, when the user taps the open trigger, the image within the adjacent div matching the class \u201cmobi-swap-content\u201d is hidden, exposing the alternative image set as the background of its containing div.<\/p>\n<p>Now when the user taps on the close trigger, the open trigger will re-appear and the original image will also become visible again. This is because tapping on any other element will cause the mobile email client to revert any hover actions taken previously \u2013 in our case we merely gave the user a space to tap on to perform it.<\/p>\n<h3>Advanced Version<\/h3>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/freshinbox.com\/blog\/wp-content\/uploads\/2014\/01\/switch-bottom1.jpg\" \/><\/p>\n<p>So far we&#8217;ve got all the components necessary to perform an image switch on a mobile client. However, having the trigger elements at the top of the image isn\u2019t ideal since the user&#8217;s finger will block the image while toggling between the two images. With a few tweaks we can move the trigger to the bottom.<\/p>\n<pre class=\"lang:default mark:17,38-41 decode:true\">&lt;style&gt;\r\n\u00a0@media screen and (max-device-width: 480px) {\r\n  div[class=mobi-swap-open], div[class=mobi-swap-close]{\r\n    display: block !important;\r\n    max-height: none !important;   \r\n    background-color: #eeeeee;\r\n    padding-left:10px;\r\n    font: 15px Helvetica;\r\n    line-height:40px;\r\n  }\r\n  div[class=mobi-swap-open] a, div[class=mobi-swap-close] a{\r\n    display:block;\r\n    text-decoration:none;\r\n  }\r\n  div[class=mobi-swap-open] {\r\n    position:absolute;\r\n    bottom:0px; \r\n    left:0px;\r\n    right:0px;\r\n  }\r\n  div[class=mobi-swap-open]:hover {\r\n    visibility: hidden;\r\n  }\r\n  div[class=mobi-swap-open]:hover + div[class=mobi-swap-content] img{\r\n    visibility:hidden;\r\n  }\r\n}\r\n&lt;\/style&gt;\r\n\r\n&lt;div style=\"position:relative;\"&gt;\r\n&lt;!--[if !mso 9]&gt;&lt;!--&gt;\r\n&lt;div class=\"mobi-swap-open\" style=\"top:auto;bottom:0px;display:none;max-height:0px;overflow:hidden;\"&gt;\r\n&lt;a href=\"#\"&gt;Tap here to show back&lt;\/a&gt;&lt;\/div&gt;\r\n&lt;!--&lt;![endif]--&gt;\r\n  &lt;div class=\"mobi-swap-content\" style=\"background-image:url(http:\/\/...\/back-image.jpg);\"&gt;\r\n  &lt;a href=\"http:\/\/..\/link\"&gt;&lt;img src=\"http:\/\/...\/front-image.jpg\" style=\"display:block;\" height=\"265\" width=\"250\" alt=\"...\" border=\"0\"&gt;&lt;\/a&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--[if !mso 9]&gt;&lt;!--&gt;\r\n&lt;div class=\"mobi-swap-close\" style=\"display:none;max-height:0px;overflow:hidden;\"&gt;\r\n  &lt;a href=\"#\"&gt;Tap here to show front&lt;\/a&gt;&lt;\/div&gt;\r\n&lt;!--&lt;![endif]--&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Here we moved the close trigger below the image but we left the open trigger above the image and modified its &#8220;top&#8221; attribute to &#8220;bottom&#8221;. This allows the open trigger to reside logically BEFORE the image (so that our \u201c+\u201d sibling selector rule will continue to work) while appearing visually after the image. Neat eh?<\/p>\n<h3>Takeaways:<\/h3>\n<ul>\n<li>The ability to display different images allow you to give recipients a way to get a better idea of how a product looks without taking up a lot of space in your email.<\/li>\n<li>Interactive elements within an email can liven up the experience for the recipient and make your email more memorable.<\/li>\n<li>Although this example only works on the iPhone and Android&#8217;s native email clients, they account for the bulk of mobile email opens.<\/li>\n<\/ul>\n<style>\n#email-me {\n  padding:10px 10px;\n  font-family: \"Lucida Grande\";<\/p>\n<p>}\n#email-me-email {\n  width:170px;\n  height:20px;\n  margin-bottom: 5px;\n  background-color: #FAFCEF;\n  font-size: 14px;\n  padding: 3px 5px;\n  display:inline-block;\n  border-radius:3px;<\/p>\n<p>}\n#email-me-btn {\n  cursor: pointer;\n  font-size: 14px;\n  color: white;\n  text-decoration:none;\n  text-shadow:#555555 0 1px;\n  background-color: #7DC442;\n  padding: 5px 12px;\n  border:1px solid #559621;\n  border-radius: 5px;\n  display:inline-block;<\/p>\n<p>}\n#email-me-btn:hover {\n  background-color: #78D62C;\n}\n#email-me{\n  width: 400px;\n  padding:20px;\n  border:3px solid #888888;\n  border-radius: 5px;\n}\n<\/style>\n<p><BR><\/p>\n<p><span style=\"color: #555555; font-style: italic;\">* There is also the \u201c~\u201d sibling selector which does not require that the sibling element be immediately after the first element.\u00a0Unfortunately this selector is not supported by Android. Also, sibling selectors are CSS3 and therefore not supported by certain old desktop email clients However in our case this is moot since we\u2019re targeting mobile clients.<\/span><\/p>\n<p><script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.10.2\/jquery.min.js\"><\/script><br \/>\n<script>\n  var theEmail = null;\n  $(document).ready(function(){\n    $('#email-me-btn').click(function(){\n      theEmail = $('#email-me-email').val();\n      sendEmail(theEmail);\n    });\n    $(\"#email-me-email\").keypress(function(e) {\n      if(e.which == 13) {\n        theEmail = $('#email-me-email').val();        \n        sendEmail(theEmail);\n      }\n    });\n  });\n  var sendEmail = function(emailAddr){\n    if(!emailAddr || emailAddr.length < 12){\n      alert('Please enter an email');\n      return;\n    }\n    $('#email-me-email').val('sending...');\n    $.ajax({\n      url: \"https:\/\/freshinbox.com\/examples\/tap-to-switch\/mail_example.php\",\n      data: {\n        email: emailAddr\n      },\n      success: function( data ) {\n        if(data == null || data.indexOf(\"ERROR\") != -1){\n          \/\/alert(data.indexOf(\"ERROR\"));\n          alert(\"There was a problem sending the email \\n\"+data);\n        }else{\n          alert(\"An email has been sent to \"+emailAddr+\"!\\n (Be sure to check the spam folder just in case)\");          \n        }\n        $('#email-me-email').val('');\n      }\n    });\n  }\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In <a title=\"3 Steps to Create an Interactive Email Using CSS\" href=\"http:\/\/freshinbox.com\/blog\/3-steps-to-create-an-interactive-email-using-css\/\" target=\"_blank\">a previous article<\/a> I showed how to create &#8220;rollover images&#8221; in email wherein the user could move their cursor over an image and an alternate image will be shown. This tutorial will show you how to create a similar effect on mobile&#8230;<\/p>\n","protected":false},"author":2,"featured_media":271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,24,25],"tags":[8,3,11,10,5,35,16,9,4,14,34,20,33,15,13,21],"_links":{"self":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":42,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":1645,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/posts\/235\/revisions\/1645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/media\/271"}],"wp:attachment":[{"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freshinbox.com\/blog\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}