Advertisement
CrouchingBruin

Using jQuery to Change an Image

Mar 12th, 2016
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 2.32 KB | None | 0 0
  1. <script type="text/javascript">
  2. <!--
  3. jQuery(document).ready(function($){
  4.    $("#div1").mouseenter(function(){
  5.        $(".phone").removeClass("showphone");
  6.        $(".image1").addClass("showphone");
  7.    });
  8.    $("#div2").mouseenter(function(){
  9.        $(".phone").removeClass("showphone");
  10.        $(".image2").addClass("showphone");
  11.    });
  12.    $("#div3").mouseenter(function(){
  13.        $(".phone").removeClass("showphone");
  14.        $(".image3").addClass("showphone");
  15.    });
  16.    $("#div4").mouseenter(function(){
  17.        $(".phone").removeClass("showphone");
  18.        $(".image4").addClass("showphone");
  19.    });
  20. });
  21. //--></script>
  22. <style>
  23. .phone {
  24.    display: none;
  25.    position: absolute;
  26.    top: 0;
  27.    right: 0;
  28. }
  29. .phone.showphone {
  30.    display: block;
  31. }
  32. #phone_container {
  33.    position: relative;
  34.    height: 650px;
  35.    float: right;
  36.    width: 75%;
  37.    box-sizing: border-box;
  38. }
  39. .mydiv {
  40.    border: 1px solid blue;
  41.    font-size: 26px;
  42.    margin-bottom: 10px;
  43.    padding: 5px;
  44.    border-radius: 3px;
  45.    box-shadow: 5px 5px rgba(0, 0, 50, 0.3);
  46.    width: 25%;
  47.    float: left;
  48.    box-sizing: border-box;
  49. }
  50. .mydiv:hover {
  51.   cursor: pointer;
  52. }
  53. </style>
  54.  
  55. <p> This post is an example of how to change an image โ€œin placeโ€ when the mouse is hovered over different areas. </p>
  56. <!--more-->
  57. <p>The image below should change as you move the mouse over the different &lt;div&gt;s. For each &lt;div&gt;, there is an event handler which captures the mouseenter event that assigns a&nbsp;<strong>showphone</strong>&nbsp;class to a specific image. The code can be seen at&nbsp;<a href="http://pastebin.com/86UmpPNa" target="_blank" rel="noreferrer noopener">this pastebin</a>.</p>
  58.  
  59. <div id="phone_container">
  60. <img class="phone image1 showphone" src="http://inrgee.com/wp-content/uploads/2019/07/app_features_phone_1.png">
  61. <img class="phone image2" src="http://inrgee.com/wp-content/uploads/2019/07/app_features_phone_2.png">
  62. <img class="phone image3" src="http://inrgee.com/wp-content/uploads/2019/07/app_features_phone_3.png">
  63. <img class="phone image4" src="http://inrgee.com/wp-content/uploads/2019/07/app_features_phone_4.png">
  64. </div>
  65. <div id="div1" class="mydiv">This is div1.</div>
  66. <div id="div2" class="mydiv">This is div2.</div>
  67. <div id="div3" class="mydiv">This is div3.</div>
  68. <div id="div4" class="mydiv">This is div4.</div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement