1

我有这段代码可以淡出并在单击相关链接时更改 div。我希望图像作为我的链接,而不仅仅是“经销商、广告等”。除此之外,虽然我会在相应的 div 处于活动状态时将图像更改为另一个图像。

举个例子。单击“dealeroff.jpg”图像时,它将变为“dealeron.jpg”,直到单击另一个图像并更改活动div。

<ul id="controlls">  
  <li><a href="#" id="one" class="dealer">Dealer</a></li>
  <li><a href="#" id="two" class="advertise">Advertise</a></li>
  <li><a href="#" id="three" class="social">Social Media</a></li>
  <li><a href="#" id="four" class="need">Need</a></li>
</ul>

 <div id="slider">
    <div id="dealer">
        <p>If you click on this paragraph
          you'll see it just fade away.
        </p>
    </div>
    <div id="advertise">
        <p>If you click on this paragraph
        you'll see it just fade away.
        </p>
    </div>
    <div id="social">
        <p>If you click on this paragraph
        you'll see it just fade away.
        </p>
    </div>
    <div id="need">
        <p>If you click on this paragraph
        you'll see it just fade away.
        </p>
    </div>
</div>

$("#controlls li a").click(function(e) {
   e.preventDefault();
     var id = $(this).attr('class'); // get class of clicked anchor tag
      // first fadeOut the visible div
$('#slider div:visible').fadeOut(500, function() {
    // after fadeOut complete show target div
    $('div#' + id).fadeIn();
  })
});

任何帮助都会很棒:)

4

2 回答 2

3

像这样的东西:

<ul id="controlls">  
    <li><img src="dealeroff.jpg"    id="one"   class="dealer" /></li>
    <li><img src="advertiseoff.jpg" id="two"   class="advertise" /></li>
    <li><img src="socialoff.jpg"    id="three" class="social" /></li>
    <li><img src="needoff.jpg"      id="four"  class="need" /></li>
</ul>

与 JS

$("#controlls li img").on('click', function(e) {
   e.preventDefault();
   var div = $('#' + this.className);

   $("#controlls li img").not(this).attr('src', function(_,source) {
       return source.replace('on', 'off');
   });

   $(this).attr('src', function(_,source) {
       if ( source.indexOf('off') != -1 ) {
           return source.replace('off', 'on');
       } else {
           return source.replace('on', 'off');
       }
   });

   $('#slider div:visible').fadeOut(500, function() {
       div.fadeIn();
   });

});
于 2013-08-17T19:56:22.317 回答
2

您可以设置一个类,例如.active根据链接是否处于活动状态,并根据设置的类定义两个不同的背景。有关示例,请参见此 JSfiddle:http: //jsfiddle.net/B398u

HTML

<ul id="controlls">  
  <li><a href="#" data-target="#dealer" id="one">Dealer</a></li>
  <li><a href="#" data-target="#social" id="two">Social Media</a></li>
  <li><a href="#" data-target="#need" id="three">Need</a></li>
</ul>

 <div id="slider">
    <div id="dealer">
        <p>Dealer paragraph</p>
    </div>
    <div id="social">
        <p>Social paragraph</p>
    </div>
    <div id="need">
        <p>Need paragraph</p>
    </div>
</div>

CSS

a#one {
    /* replace `red` with `url('imageoff.png');` */
    background: red;
}
a#one.active {
    /* replace `blue` with `url('imageon.png');` */
    background: blue;
}
a#two {
    background: red;
}
a#two.active {
    background: blue;
}
a#three {
    background: red;
}
a#three.active {
    background: blue;
}

Javascript

$(document).ready(function() {
$("#controlls li a").click(function(e) {
    e.preventDefault();
    $this = $(this);

    var id = $this.data('target');

    $('#controlls li a').removeClass('active');
    $this.addClass('active');

    $('#slider div').hide().filter(id).show();
});
});
于 2013-08-17T19:58:54.230 回答