Help in refactoring jquery code

Asked

Viewed 124 times

6

$(".img1").mouseenter(function() {
    $(this).css('box-shadow', "inset 0 0 20px black"); 
});

$(".img1").mouseout(function(){
    $(this).css('box-shadow', "");
});

$(".img2").mouseenter(function() {
    $(this).css('box-shadow', "inset 0 0 20px black");  
});

$(".img2").mouseout(function(){
    $(this).css('box-shadow', "");
});

$(".h2").mouseenter(function(){
    $('.img2').css("box-shadow","inset 0 0 20px black");
});

$(".h2").mouseout(function(){
    $('.img2').css("box-shadow", '');
});

$(".h1").mouseenter(function(){
    $('.img1').css("box-shadow","inset 0 0 20px black");
});

HTML

<div class="box first">
  <div >
    <img class="img1" />
  </div>
  <a href="t1.html"><h3 class="h1"> Aliens </h3></a>
  <div>
    <p></p>
  </div>
</div>
<div class="box second">
  <div>
    <img class="img2" />
  </div>
  <a href="a2.html"><h3 class="h2"> Day of the Dead </h3></a>
  <p></p>
</div>
<div class="box third">
  <div>
    <img class="img3" />
  </div>
  <a href="a3.html"><h3 class="h3"> Evil dead </h3></a>
  <p></p>
</div>

How do I make this code shorter to give for more images without always repeating the same. http://omeufilme.esy.es/teste2.html Here’s my site, there’s that white border around the images, how do I get it out?

  • 1

    Can you show the structure of your HTML? I’m happy to answer but can’t help much without seeing the HTML and understanding the default/ architecture of the code.

  • The term "compress" was not adequate. The right one would be "reduce redundancies", "eliminate redundancies". The term "compress" gives an idea of compressing, or minifying the code, which is very different.

  • I repeat that the lack of HTML creates answers to "guess" the problem. The ideal would be to put the HTML for more correct answers that optimize the code.

  • in the next doubt I will take this into account, thanks for the tip.

  • @Peter could have gathered here any way. The question remains incomplete for all the others who for years have seen this question... I will put it together in a little while and in return I will give an answer too.

  • http://answall.com/questions/101823/aparecimento-de-border-n%C3%A3o-relatada here a new topic I opened, is the continuation where I was able to solve this problem but another appeared.

  • @Joãopedro added a new answer here although you already have another accepted. To answer in the other you have to explain the problem better. With an image of the behavior you refer to or an example in jsFiddle with images and the problem appearing. This way the help is more accurate and fast. "win win"

Show 2 more comments

4 answers

4


you can use a property data-* to store the image selector, then just have a bind for all the elements.

var images = $("[data-img]");
images.mouseenter(function() {
  $(this.dataset.img).css('box-shadow', "inset 0 0 20px black"); 

});

images.mouseout(function(){
  $(this.dataset.img).css('box-shadow', "");
});
img {
  height: 240px;
  width: 240px;
  border: 1px solid black;
}

h1 {
  float: left;
  margin-right: 5px;
  width: 240px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img class="img1" data-img=".img1" />
<img class="img2" data-img=".img2" />
<h1 class="h1" data-img=".img1" >Image 01</h1>
<h1 class="h2" data-img=".img2" >Image 02</h1>

If you prefer, you may not use jQuery, follow the same code with Vanilla Javascript. It also seems to me that you are using classes where Ids would be ideal, so I also made this modification, finally, instead of editing the inline style/css, I am adding/removing a class to apply the shadow.

var images = document.querySelectorAll("[data-img]");

var onImgMouseEnter = function (event) {
  var img = document.getElementById(event.target.dataset.img);
  img.classList.add("sombra");
};

var onImgMouseOut = function (event) {
  var img = document.getElementById(event.target.dataset.img);
  img.classList.remove("sombra");
};
    
[].forEach.call(images, function (image, indice) {  
  image.addEventListener("mouseenter", onImgMouseEnter);
  image.addEventListener("mouseout", onImgMouseOut);
});
img {
  height: 240px;
  width: 240px;
  border: 1px solid black;
}

h1 {
  float: left;
  margin-right: 5px;
  width: 240px;
  border: 1px solid black;
}

.sombra {
  box-shadow: inset 0 0 20px black
}
<img id="img1" data-img="img1" />
<img id="img2" data-img="img2" />
<h1 id="h1" data-img="img1" >Image 01</h1>
<h1 id="h2" data-img="img2" >Image 02</h1>

  • I’m using the first code and it’s doing all right but I have one question, it’s adding a white border to the image I don’t intend. because it happens and how I can change it?

  • Could someone enlighten me -1?

3

You are using more than one class to do the same thing, the class is used to pick up various elements and do the same thing unlike the id that is unique on the page. You don’t need to create multiple classes for what you want to do, just one. Follow the example: HTML :

<h2 class="elements">Titulo</h1>
<img src="img1.jpg" class="elements" >
<img src="img2.jpg" class="elements" >

jQuery

$(".elements").mouseenter(function() {
   $(this).css('box-shadow', "inset 0 0 20px black"); 
  });

$(".elements").mouseout(function(){

     $(this).css('box-shadow', "");

  });
  • I am also in favor of a class only, to represent Who I am wanting to apply the effect. Otherwise the parade saw mess

0

To compress this code, you can do the following:

function eventInElement(elEvent, elCSS, evento) {
      $(elEvent).on(evento, function() {
           elCSS = (elCSS == null) ? this : elCSS;
           if (evento == 'mouseenter') {
              $(elCSS).css("box-shadow","inset 0 0 20px black");
           } else {
              $(elCSS).css("box-shadow","");
           }
      });
}

eventInElement('.img1,.img2', null, 'mouseenter');
eventInElement('.img1,.img2', null, 'mouseout');
eventInElement('.h2', '.img2', 'mouseenter');
eventInElement('.h1', '.img1', 'mouseenter');

You can use the website Javascript Minifer or other similar to minify your file, here the example of the above code:

function eventInElement(e,n,t){$(e).on(t,function(){n=null==n?this:n,"mouseenter"==t?$(n).css("box-shadow","inset 0 0 20px black"):$(n).css("box-shadow","")})}eventInElement(".img1,.img2",null,"mouseenter"),eventInElement(".img1,.img2",null,"mouseout"),eventInElement(".h2",".img2","mouseenter"),eventInElement(".h1",".img1","mouseenter");

0

Assuming HTML matches your new question you can do so without having to change HTML:

$('.box').on('mouseenter mouseout', 'img, h3', function (e) {
  var shadow = e.type == 'mouseenter' ? 'inset 0 0 20px black' : '';
  $(this).closest('.box').find('img').css('box-shadow', shadow);
});

jsFiddle: http://jsfiddle.net/zg33wh7h/

This is not necessarily the fastest way, as it depends on jQuery, but is the most compressed.

But the best way can only be with CSS. Less code, faster. In that case you can do so by listening to the mouse throughout the .box:

.box:hover img {
  box-shadow: inset 0 0 20px black
}

jsFiddle: http://jsfiddle.net/zg33wh7h/1/

  • 1

    I found your answer and miguel’s answer the best. If jQuery is made to simplify, you don’t have to write a lot of code to do a simple thing.

  • 1

    It would be interesting to know why they gave me -1... envy? reprisal? or anything in the answer you want to point to me getting better? :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.