简体   繁体   English

onclick功能淡入淡出内容轮播

[英]onclick function fade in fade out content carousel

Evening, 晚间,

i'm having trouble with onclick fade in and fade out animation on some content this is the html script for the navigation: 我在使用onclick淡入和淡出某些内容的动画时遇到麻烦,这是导航的html脚本:

<div class="col s12 m4">

    <h4 class="pict-menu"><a href="#!" id="type1" class="waves-effect waves-default">type1</a></h4>
    <h4 class="pict-menu"><a href="#!" id="type2" class="waves-effect waves-default">type2</a></h4>

</div>

and this is the content 这就是内容

<div class="col s12 m8 owl-carousel owl-theme" id="type-1">
    <?php foreach($regent as $row): ?>
    <div class="item"><img class="responsive-img" src="<?php echo base_url(); ?>admin/assets/images/media/<?php echo $row->media_file; ?>" alt="<?php echo $row->media_title; ?>"></div>
    <?php endforeach; ?>
</div>

<div class="col s12 m8 owl-carousel owl-theme" id="type-2">
    <div class="item"><img class="responsive-img" src="<?php echo base_url(); ?>assets/serenia/images/slide1.jpg" alt=""></div>
    <div class="item"><img class="responsive-img" src="<?php echo base_url(); ?>assets/serenia/images/slide2.jpg" alt=""></div>
</div>

the javascripts JavaScripts

function type_click() {
    $("#type1").click(function() {
        $("#type-1").fadeIn(300);
        $("#type-2").fadeOut(300);
    });

    $("#type2").click(function() {
        $("#type-2").fadeIn(300);
        $("#type-1").fadeOut(300);
    });
}
$(function() {type_click()});

this is working fine when i'm just using 2 content, but it's not working properly when i tried to use more than 2 content. 当我仅使用2种内容时,此方法工作正常,但当我尝试使用2种以上内容时,此方法无法正常工作。 Would appreciate any help. 将不胜感激。 Thanks in advance. 提前致谢。

Try this: 尝试这个:

https://jsfiddle.net/y6xjuoba/ https://jsfiddle.net/y6xjuoba/

HTML HTML

<div class="col s12 m4">

  <h4 class="pict-menu"><a href="#!" id="type-1" class="waves-effect waves-default">type1</a></h4>
  <h4 class="pict-menu"><a href="#!" id="type-2" class="waves-effect waves-default">type2</a></h4>
  <h4 class="pict-menu"><a href="#!" id="type-3" class="waves-effect waves-default">type3</a></h4>
  <h4 class="pict-menu"><a href="#!" id="type-4" class="waves-effect waves-default">type4</a></h4>

</div>



<div class="col s12 m8 owl-carousel owl-theme" id="type-2">
  <div class="item type-1">test1</div>
  <div class="item type-2">test2</div>
  <div class="item type-3">test3</div>
  <div class="item type-4">test4</div>
</div>

JS: JS:

function type_click() {
  $(".waves-effect").click(function() {
    var clicked = $(this).hasClass("clicked")
    if (clicked !== true) {
      var currImage = '.' + $(this).attr("id"); + '"'
      $(".waves-effect").removeClass("clicked");
      $(this).addClass("clicked");

      $(".item").fadeOut(300);
      $(currImage).fadeIn(300);
    }

  });

}
$(function() {
  type_click()
});

It's kind of messy, but it should work for any number of images. 有点杂乱无章,但它可以处理任意数量的图像。 just make sure the class of the carousel item divs is equivalent to the id of the pict-menu links. 只需确保轮播项目divs的类等于pict-menu链接的id。 If you have a large number of images to carousel through you may want to consider some kind of templating solution like Handlebars. 如果您要传送大量图像,则可能需要考虑某种模板解决方案,例如Handlebars。

$(".owl-theme").click(function() { //select by class
    var clicked  = $(this);
    clicked.fadeIn(300); //fade selected element in
    $('.owl-theme').not(clicked).fadeOut(300); //fade everything except clicked element out
});

https://jsfiddle.net/zkqf6mjz/ https://jsfiddle.net/zkqf6mjz/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM