循环 div 和缩略图

2023-11-29前端开发问题
9

本文介绍了循环 div 和缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望有人能救我!我正在尝试为我的页面创建一个(希望如此)简单的旋转横幅.我有 7 个包含照片和文字的 div,它们如下:

I'm hoping someone can save my me! I'm trying to create a (hopefully) simple rotating banner for my page. I have 7 divs that contain photos and text, they are as follows:

<div id="content-1">Sample text</div>
<div id="content-2">Sample text</div>
<div id="content-3">Sample text</div>
<div id="content-4">Sample text</div>
<div id="content-5">Sample text</div>
<div id="content-6">Sample text</div>
<div id="content-7">Sample text</div>

在这些 div 下方,我有 7 个相应的 div 缩略图:

And below those divs I have 7 corresponding divs that are thumbnails:

<div id="thumb-content-1">Sample text</div>
<div id="thumb-content-2">Sample text</div>
<div id="thumb-content-3">Sample text</div>
<div id="thumb-content-4">Sample text</div>
<div id="thumb-content-5">Sample text</div>
<div id="thumb-content-6">Sample text</div>
<div id="thumb-content-7">Sample text</div>

我想做几件事:

1) 每 5 秒循环通过一个新的 div(因此content-1"会显示 5 秒,然后是content 2"等.

1) Every 5 seconds cycle through a new div (so "content-1" would display for 5 seconds, then "content 2" etc.

2) 为当前缩略图应用一个名为cr-rotator"的类.我已经设置了样式.

2) Apply a class to the current thumbnail called "cr-rotator". I have the style already setup.

3) 当用户将鼠标悬停在主 div 或缩略图 div 上时,我希望能够暂停它的旋转.

3) I would like to be able to pause it from rotating when a user hovers over either the main div or thumbnail div.

4) 最后,我想拥有它,这样如果您将鼠标悬停在缩略图上,它会更改主要内容,然后在您关闭鼠标时继续循环.例如,假设您将鼠标悬停在thumb-content-3"上,它将使 divcontent-3"可见,然后当您将鼠标移出时,它将继续循环.

4) Lastly, I would like to have it so that if you hover over a thumbnail it would change the main content, then continue cycling when you mouse off. So say for example you hover over 'thumb-content-3' it will make the div 'content-3' visible and then when you mouse out it will continue cycling.

我知道这里有很多要求,我提前感谢任何可以帮助我的人.我已经获得了一个脚本来循环浏览主要图像,但我不确定如何实现其余部分:

I understand there is a lot demanded here and I thank in advance anyone who can help me out. I have been provided a script to cycle through the main images but I'm not sure how to implement the rest:

var divs = $('div[id^="content-"]').hide(),
    i = 0;

(function cycle() { 
    divs.eq(i).fadeIn(200)
              .delay(3000)
              .fadeOut(200, cycle);

    i = ++i % divs.length; // increment i, 
                           //   and reset to 0 when it equals divs.length
})();

非常感谢任何可以帮助我的人.

Thank you so much to anyone that can help me.

推荐答案

Fade rotator - jsBin 演示

<div id="rotator">
  
  <div id="slides">
    <div>Sample text 1</div>
    <div>Sample text 2</div>
    <div>Sample text 3</div>
  </div>

  <div id="thumbs">
    <div>1</div>
    <div>2</div>
    <div>3</div>   
  </div>
  
</div>

  • 自动淡入淡出
  • 悬停时暂停
  • 悬停在拇指上会触发主幻灯片
  • 在停止的地方重新启动
  • 还有jQ代码:

    var $el = $('#fader'),
        
    //  SETUP  ////////
        F = 600 ,    // Fade Time
        P = 2000 ,   // Pause Time
        C = 0 ,      // Counter / Start Slide# (0 based)
    ///////////////////
        
        $sl = $('#slides > div'),
        $th = $('#thumbs > div'),
        N = $sl.length,
        T = null;
    
    $sl.hide().eq(C).show();
    $th.eq(C).addClass('on');
    
    // ANIMATION
    function anim() { 
      $sl.eq(C%N).stop(1).fadeTo(F,1).siblings().fadeTo(F,0);
      $th.removeClass('on').eq(C%N).addClass('on');
    }
    
    // AUTO ANIMATE     
    function autoAnim() {   
       T = setTimeout(function() {
          C++;
          anim();     // Animate
          autoAnim(); // Prepare another iteration
       }, P+F);
    }
    autoAnim();      // Start loop
    
    // HOVER PAUSE
    $el.hover(function(e) {
       return e.type==='mouseenter'? clearTimeout( T ) : autoAnim();
    });
    
    // HOVER THUMBNAILS
    $th.on('mouseenter', function() {
       C = $th.index( this );
       anim();
    });
    

    这篇关于循环 div 和缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5