如何在jquery

时间:2016-01-25 14:36:27

标签: jquery image slider switch-statement

我有这段代码:

$(document).ready(function() {

  var tid = 0;
  $c = 0;
  $immagine = $(".lazy"); //salvo la picuture visualizzata
  $rect = $(".active"); //salvo il rettangolo attivo
  $rect_first = $(".active");

  function loop() {
    $rect.next().attr("class", "active");
    $rect.attr("class", "");
    $rect = $rect.next();

    $immagine.next().attr("style", "vivibility: visible;");
    $immagine.attr("style", "visibility: hidden;");
    $immagine = $immagine.next();

    $c++;
    if ($c > 7) {
      goFirst();
    }
    tid = setTimeout(loop, 1500);
  }

  function goFirst() {
    $rect.attr("class", "");
    $rect_first.attr("class", "active");
    $rect = $rect_first;

    $immagine.attr("style", "visibility: hidden;");
    $immagine = $(".lazy");
    $immagine.attr("style", "visibility: visible;");
    $c = 0;
  }

  function handleMouseOver() {
    tid = setTimeout(loop, 1500);
  }

  $("span.miniatura")
    .one("mouseover", handleMouseOver) //questa funzione è chiamata una volta sola
    .on("mouseleave", function() { //questa può essere chiamata più volte
      clearTimeout(tid);
      $(this).one("mouseover", handleMouseOver)
      goFirst();
    });
})
@font-face {
  font-family: 'vinegar_strokeregular';
  src: url('vinegar_stroke-webfont.eot');
  src: url('vinegar_stroke-webfont.eot?#iefix') format('embedded-opentype'), url('vinegar_stroke-webfont.woff2') format('woff2'), url('vinegar_stroke-webfont.woff') format('woff'), url('vinegar_stroke-webfont.ttf') format('truetype'), url('vinegar_stroke-webfont.svg#vinegar_strokeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}
/*POPUP CONTATTACI*/

#thumb {
  cursor: pointer;
}
#pop_background {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.8;
  z-index: 1000;
  cursor: pointer;
  display: none;
}
#pop_box {
  position: absolute;
  background: #fff;
  background-color: white;
  border-top: 4px solid #f4850f;
  border-radius: 5px;
  width: 30%;
  padding-bottom: 6px;
  margin-top: 8%;
  margin-left: 35%;
  z-index: 1500;
  display: none;
  text-align: center;
}
#pop_box form {
  padding-top: 3%;
}
#pop_box form label {
  display: block;
  padding-top: 1%;
}
#pop_box textarea {
  width: 60%;
  resize: none;
}
#pop_box input {
  width: 40%;
}
#pop_box #mex {
  margin-left: -4.5%;
}
#close_button {
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid #999;
  text-align: center;
  line-height: 30px;
  font-size: 30px;
  float: right;
}
/*FINE POPOUP*/

aside {
  display: block;
  float: right;
  width: 19%;
  border: 2px;
  border-color: red;
  border-style: ridge;
}
#ricerca {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 20%;
  min-width: 150px;
}
.srcbar {
  height: 28px;
  -moz-border-radius: 14px;
  -webkit-border-radius: 14px;
  border-radius: 14px;
  font-size: 14px;
  color: #fff;
  width: 100%;
}
input {
  outline: none;
}
#buttonsearch {
  cursor: pointer;
  width: 70px;
  padding: 0px;
  border: 0;
  background-color: #F6A957;
  font-weight: bold;
  position: absolute;
  top: 0;
  right: 0;
}
#s {
  padding: 0 0 0 10px;
  background: #333;
  border: 0 none;
  background-color: #414141;
}
#login {
  position: absolute;
  top: 60px;
  right: 20px;
  width: 90px;
  -moz-border-radius: 30px;
  -webkit-border-radius: 30px;
  border-radius: 30px;
  color: #fff;
  background-color: #414141;
}
#login a {
  text-decoration: none;
  font-size: 15px;
  font-family: verdana;
  color: grey;
  margin-top: 2px;
}
#login a:hover {
  color: #F6A957;
  font-weight: bold;
}
#login img {
  margin-left: 10px;
  margin-top: 5px;
}
#logo {
  height: 80px;
  max-width: 400px;
  width: 30%;
  position: relative;
  top: 10px;
}
#top {
  background-image: url(http://bicicletteria.altervista.org/image/Immagine_4.png);
  background-repeat: repeat;
  display: block;
  margin-left: -8px;
  margin-top: -20px;
  margin-right: -8px;
}
#topmenu li {
  display: inline;
}
#topmenu li a {
  display: inline-block;
  list-style-type: none;
  text-align: center;
  padding: 12px 24px;
  color: white;
  text-decoration: none;
  font-size: 20px;
  font-family: verdana;
}
#topmenu ul {
  list-style-type: none;
  margin: 0px auto;
  padding: 0;
  overflow: hidden;
  background-color: #414141;
  text-align: center;
}
#topmenu li a:hover {
  color: #F6A957;
  background-color: #111;
}
body {
  background-image: url(http://i.imgur.com/Eor57Ae.jpg);
  margin-bottom: -5px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed;
}
#container {
  display: block;
  width: 62%;
  float: left;
  background-color: rgba(255, 255, 255, 0.6);
  margin-left: 19%;
  margin-bottom: 10px;
  border: 1px solid black;
}
#novita h1,
#piu_venduti h1 {
  position: relative;
  font-family: "vinegar_strokeregular";
}
#container header {
  text-align: center;
}
#novita .products {
  margin-left: 35%;
  margin-right: 35%;
  margin-bottom: 5%;
}
#piu_venduti article {
  display: inline-block;
}
/*SLIDER IMMAGINI*/

#miniatura {
  position: relative;
}
#min {
  width: 90%;
  text-align: center;
}
#min img {
  border: 1px solid #646464;
}
ul.vid-rotater li {
  color: white;
}
.frame {
  display: block;
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
  margin-top: 7px;
  z-index: 3;
}
.frame img {
  border: 1px solid #FFFFFF;
}
.pic {
  clear: both;
  cursor: pointer;
  display: block;
  position: relative;
  margin-left: 2.5%;
  margin-right: 2.5%;
  width: 95%;
}
.pic .frame {
  left: 0px;
  position: absolute;
  top: 0px;
}
.pic img {
  position: absolute;
  top: 10px;
  z-index: 2;
  width: 95%;
  left: 2.5%;
  right: 2.5%;
}
.pic:hover {
  text-decoration: none;
}
.vid-rotater {
  position: absolute;
  visibility: visible;
  margin: 0 0 0 25px;
  padding: 7px 5px 5px;
  left: 0px;
}
#min ul {
  position: absolute;
  width: 80%;
  margin-left: 10%;
  margin-right: 10%;
}
.vid-rotater li,
.vid-rotater a {
  display: block;
  float: left;
  width: 29px;
}
.vid-rotater li {
  margin: 0 0 0 2px;
  padding: 0;
}
.vid-rotater a {
  overflow: hidden;
  padding: 4px 0 3px;
}
.vid-rotater span {
  background: #FFFFFF none repeat scroll 0 0;
  border-bottom: 1px solid #333333;
  border-right: 1px solid #333333;
  display: block;
  height: 3px;
  line-height: 999em;
  overflow: hidden;
}
.vid-rotater .active span {
  background: #F6A957 none repeat scroll 0 0;
  border-color: #660033;
  cursor: pointer;
}
/*BOX PRODOTTI*/

.products {
  background-color: RGBa(65, 65, 65, 0.4);
  width: 30%;
  margin-top: 1.25%;
  margin-bottom: 1.25%;
  margin-left: 2.25%;
  border-radius: 10px;
  border: 1px groove #999;
  box-shadow: 1px 1px 1px #BBB;
}
.descrizione {
  padding-top: 65%;
}
.products .miniatura {
  position: relative;
  left: 2.5%;
  right: 2.5%;
  width: 90%;
}
.products p {
  color: white;
  text-align: center;
  margin-top: -5%;
}
footer {
  clear: both;
  display: block;
  padding: 15px;
  background-image: url(http://bicicletteria.altervista.org/image/Immagine_4.png);
  background-repeat: repeat;
  background-color: #242424;
  margin-left: -8px;
  margin-right: -8px;
  color: #FFF;
  font-size: 12px;
  font-family: monospace;
  font-weight: normal;
  text-align: center;
}
footer dl,
dd {
  color: #F6A957;
  margin: 0 auto;
  text-align: center;
  text-decoration: none;
}
/*CHI SIAMO*/

#who h2 {
  text-align: center;
  font-family: "vinegar_strokeregular";
}
#who {
  width: 100%;
  height: 45%;
}
#iframe {
  border: 1px solid #000;
  width: 48%;
  display: inline-block;
  min-height: 350px;
  padding-left: 2%;
  padding-bottom: 2%;
}
#contattaci {
  display: inline-block;
  position: relative;
  width: 48%;
  float: right;
  padding-left: 2%;
}
#contattaci a {
  display: inline;
}
#email {
  display: inline;
}
#who p {
  text-align: justify;
  padding: 2%;
}
/*LOGIN*/

.login-block {
  width: 33%;
  padding: 5%;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 5px;
  border-top: 4px solid #F6A957;
  margin: 0 auto;
  margin-top: 20%;
  margin-bottom: 15%;
}
.login-block h1 {
  text-align: center;
  color: #000;
  font-size: 18px;
  margin-top: 0;
  margin-bottom: 10%;
}
.login-block input {
  width: 100%;
  height: 42px;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #ccc;
  margin-bottom: 10%;
  font-size: 14px;
  font-family: Montserrat;
  padding: 0 20px 0 50px;
  outline: none;
}
.login-block input#username {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login-block input#username:focus {
  background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login-block input#password {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
  background-size: 16px 80px;
}
.login-block input#password:focus {
  background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
  background-size: 16px 80px;
}
.login-block input:active,
.login-block input:focus {
  border: 1px solid #F6A957;
}
.login-block button {
  width: 100%;
  height: 40px;
  background: #F6A957;
  box-sizing: border-box;
  border-radius: 5px;
  color: #fff;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: Montserrat;
  outline: none;
  cursor: pointer;
}
#send {
  width: 40%;
  background: #F6A957;
  box-sizing: border-box;
  border-radius: 5px;
  color: #fff;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: Montserrat;
  outline: none;
  cursor: pointer;
}
#add {
  width: 40%;
  /*min-width: 90px;*/
  background: #F6A957;
  box-sizing: border-box;
  border-radius: 5px;
  color: #fff;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 14px;
  font-family: Montserrat;
  outline: none;
  cursor: pointer;
}
.login-block button:hover,
#send:hover,
#add:hover {
  box-shadow: 5px 5px 0px #999;
}
/*PAGINA PRODOTTO*/

#up {
  display: block;
  width: 100%;
}
#down {
  display: block;
  text-align: center;
  font-size: 1.2em;
  clear: left;
  width: 100%;
}
#sinistra {
  float: left;
  width: 20%;
  padding-top: 1.5%;
}
#sinistra img {
  width: 60%;
  padding-left: 20%;
}
#centro {
  float: left;
  width: 60%;
}
#centro img {
  width: 98%;
  padding-left: 2%;
  padding-top: 2%;
}
#destra {
  float: left;
  width: 20%;
  text-align: center;
}
#dimensione {
  width: 30%;
  min-width: 50px;
}
#qnt {
  width: 10%;
  min-width: 25px;
}
/*MODIFICA QUANTITA*/

.quantity .oper {
  display: block;
  float: left;
  width: 19px;
  height: 19px;
  margin-top: 6px;
}
.choose_quantity {
  padding-top: 6px;
  padding-bottom: 6px;
}
.quantity input {
  width: 45px;
  height: 18px;
  border: solid 1px #b8b8b8;
  background: #fff;
  text-align: center;
  float: left;
  margin: 5px 3px 0;
  text-rendering: auto;
  color: initial;
  letter-spacing: normal;
  word-spacing: normal;
  text-transform: none;
  text-indent: 0px;
  text-shadow: none;
  display: inline-block;
  text-align: start;
  margin: 0em 0em 0em 0em;
  font: 13.3333px Arial;
}
<div id="container">
  <section id="novita">
    <header>
      <h1>NOVITA'</h1>
    </header>
    <article class="products">
      <span class="miniatura">
                <span id="min" class="pic">
                  <ul class="vid-rotater" style="visibility: visible;">
                         <li class="active"><span>0</span>
      </li>
      <li class=""><span>1</span>
      </li>
      <li class=""><span>2</span>
      </li>
      <li class=""><span>3</span>
      </li>
      <li class=""><span>4</span>
      </li>
      <li class=""><span>5</span>
      </li>
      <li class=""><span>6</span>
      </li>
      <li class=""><span>7</span>
      </li>
      </ul>
      <a class="frame" href="http://www.cycling.it/" target="_blank" style="text-decoration: none;">
        <img class="lazy" src="http://www.cycling.it/foto_gallery/big/01-revelator_prestige_Di2_7805.jpg" data-original="" style="display: block; visibility: visible;">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw" style="visibility: hidden;">
        <img src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg" style="visibility: hidden;">
        <img src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png" style="visibility: hidden;">
        <img src="http://www.loriscycles.com/docs/gallery/2/bici_sx_101.png" style="visibility: hidden;">
        <img src="http://www.bikeitalia.it/wp-content/uploads/2014/04/bici-da-corsa-da-donna-stella-wilier.jpg" style="visibility: hidden;">
        <img src="http://www.wildpigs.it/wp-content/uploads/2012/08/status.jpg" style="visibility: hidden;">
        <img src="http://www.milkywayshop.it/media/catalog/product/cache/2/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bici-fixed-margot-cycling-margot-dragonfly-12165.jpg" style="visibility: hidden;">
      </a>
      </span>
      </span>
      <section class="descrizione">
        <p class="modello">bici mfha536
          <br>
        </p>
        <p class="marca">KTM
          <br>
        </p>
        <p class="prezzo">3000.00 €
          <br>
        </p>
      </section>
    </article>
  </section>

  <section id="piu_venduti">
    <header>
      <h1>I PIU' VENDUTI</h1>
    </header>
    <article class="products">
      <span class="miniatura">
                <span id="min" class="pic">
                  <ul class="vid-rotater" style="visibility: visible;">
                         <li class="active"><span>0</span>
      </li>
      <li class=""><span>1</span>
      </li>
      <li class=""><span>2</span>
      </li>
      <li class=""><span>3</span>
      </li>
      <li class=""><span>4</span>
      </li>
      <li class=""><span>5</span>
      </li>
      <li class=""><span>6</span>
      </li>
      <li class=""><span>7</span>
      </li>
      </ul>
      <a class="frame" href="http://www.cycling.it/" target="_blank" style="text-decoration: none;">
        <img class="lazy" src="http://www.cycling.it/foto_gallery/big/01-revelator_prestige_Di2_7805.jpg" data-original="" style="display: block; visibility: visible;">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw" style="visibility: hidden;">
        <img src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg" style="visibility: hidden;">
        <img src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png" style="visibility: hidden;">
        <img src="http://www.loriscycles.com/docs/gallery/2/bici_sx_101.png" style="visibility: hidden;">
        <img src="http://www.bikeitalia.it/wp-content/uploads/2014/04/bici-da-corsa-da-donna-stella-wilier.jpg" style="visibility: hidden;">
        <img src="http://www.wildpigs.it/wp-content/uploads/2012/08/status.jpg" style="visibility: hidden;">
        <img src="http://www.milkywayshop.it/media/catalog/product/cache/2/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bici-fixed-margot-cycling-margot-dragonfly-12165.jpg" style="visibility: hidden;">
      </a>
      </span>
      </span>
      <section class="descrizione">
        <p class="modello">bici mfha536
          <br>
        </p>
        <p class="marca">KTM
          <br>
        </p>
        <p class="prezzo">3000.00 €
          <br>
        </p>
      </section>
    </article>
    <!--secondo protodototo più venduto-->
    <article class="products">
      <span class="miniatura">
                  <span id="min" class="pic">
                    <ul class="vid-rotater" style="visibility: visible;">
                           <li class="active"><span>0</span>
      </li>
      <li class=""><span>1</span>
      </li>
      <li class=""><span>2</span>
      </li>
      <li class=""><span>3</span>
      </li>
      <li class=""><span>4</span>
      </li>
      <li class=""><span>5</span>
      </li>
      <li class=""><span>6</span>
      </li>
      <li class=""><span>7</span>
      </li>
      </ul>
      <a class="frame" href="http://www.cycling.it/" target="_blank" style="text-decoration: none;">
        <img class="lazy" src="http://www.cycling.it/foto_gallery/big/01-revelator_prestige_Di2_7805.jpg" data-original="" style="display: block; visibility: visible;">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw" style="visibility: hidden;">
        <img src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg" style="visibility: hidden;">
        <img src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png" style="visibility: hidden;">
        <img src="http://www.loriscycles.com/docs/gallery/2/bici_sx_101.png" style="visibility: hidden;">
        <img src="http://www.bikeitalia.it/wp-content/uploads/2014/04/bici-da-corsa-da-donna-stella-wilier.jpg" style="visibility: hidden;">
        <img src="http://www.wildpigs.it/wp-content/uploads/2012/08/status.jpg" style="visibility: hidden;">
        <img src="http://www.milkywayshop.it/media/catalog/product/cache/2/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bici-fixed-margot-cycling-margot-dragonfly-12165.jpg" style="visibility: hidden;">
      </a>
      </span>
      </span>
      <section class="descrizione">
        <p class="modello">bici mfha536
          <br>
        </p>
        <p class="marca">KTM
          <br>
        </p>
        <p class="prezzo">3000.00 €
          <br>
        </p>
      </section>
    </article>
    <!--terzo protodototo più venduto-->
    <article class="products">
      <span class="miniatura">
                    <span id="min" class="pic">
                      <ul class="vid-rotater" style="visibility: visible;">
                             <li class="active"><span>0</span>
      </li>
      <li class=""><span>1</span>
      </li>
      <li class=""><span>2</span>
      </li>
      <li class=""><span>3</span>
      </li>
      <li class=""><span>4</span>
      </li>
      <li class=""><span>5</span>
      </li>
      <li class=""><span>6</span>
      </li>
      <li class=""><span>7</span>
      </li>
      </ul>
      <a class="frame" href="http://www.cycling.it/" target="_blank" style="text-decoration: none;">
        <img class="lazy" src="http://www.cycling.it/foto_gallery/big/01-revelator_prestige_Di2_7805.jpg" data-original="" style="display: block; visibility: visible;">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw" style="visibility: hidden;">
        <img src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg" style="visibility: hidden;">
        <img src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png" style="visibility: hidden;">
        <img src="http://www.loriscycles.com/docs/gallery/2/bici_sx_101.png" style="visibility: hidden;">
        <img src="http://www.bikeitalia.it/wp-content/uploads/2014/04/bici-da-corsa-da-donna-stella-wilier.jpg" style="visibility: hidden;">
        <img src="http://www.wildpigs.it/wp-content/uploads/2012/08/status.jpg" style="visibility: hidden;">
        <img src="http://www.milkywayshop.it/media/catalog/product/cache/2/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bici-fixed-margot-cycling-margot-dragonfly-12165.jpg" style="visibility: hidden;">
      </a>
      </span>
      </span>
      <section class="descrizione">
        <p class="modello">bici mfha536
          <br>
        </p>
        <p class="marca">KTM
          <br>
        </p>
        <p class="prezzo">3000.00 €
          <br>
        </p>
      </section>
    </article>

  </section>
</div>

链接到jsFiddle

我想更改鼠标悬停的img图像,但是使用我的jQuery代码,我更改了页面中所有img的图像。错误在哪里? 谢谢你们!

2 个答案:

答案 0 :(得分:0)

如果你想捕捉鼠标悬停的元素,那就使用与此类似的东西:http://jsbin.com/udeha4/15/edit?html,css,js,output并在适当的元素上调用更改图像功能。

答案 1 :(得分:0)

我创建了一个jsFiddle,其中包含您的代码更新。我只改变了一部分,特别是:

$immagine = null;
$rect = null;

这两个变量在鼠标悬停时初始化。