ITPub博客

首页 > IT职业 > IT图书 > jQuery jflow无缝滚动代码及实例

jQuery jflow无缝滚动代码及实例

翻译 IT图书 作者:chunzhisu 时间:2014-07-28 11:17:00 0 删除 编辑
  • jquery jflow无缝滚动代码及实例,无缝滚动原理其实就是当滚动到最后一张的时候,让第一个排到最后一张的后边,其实也就是下一张的位置,代码还有优化的空间,高手有意可继续完善代码。

点击(此处)折叠或打开

  1. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
  2. <html xmlns=\"http://www.w3.org/1999/xhtml\">
  3. <head>
  4. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
  5. <title>jFlow Plus v2</title>
  6. <style>
  7. #sliderContainer { width: 940px; height: 300px; position: relative; border-top: 5px solid #999; border-bottom: 5px solid #999; margin: 0 auto; }
  8. .slideContent { background:url(\"/jscss/demoimg/201303/contentBg.png\") repeat scroll 0 0 transparent; position: absolute; bottom:0px; right:0px; padding: 10px; width: 920px; }
  9. .slide h3 { font: 26px/1em \'Rokkitt\', serif; color: #FFF; margin: 0 0 5px 0; }
  10. .slide p { font-size: 14px; color: #FFF; margin: 0; }
  11. .jFlowSlideContainer { overflow: hidden; }
  12. .jFlowSlideContainer a { color: #F90; text-decoration: none; font-style: italic; }
  13. .jFlowSlideContainer a:hover { text-decoration: none; }
  14. .jFlowSlideContainer a:focus { position: relative; top: 1px; }
  15. .jFlowNext { background:url(\"/jscss/demoimg/201303/arrows.png\") no-repeat scroll 0 -35px transparent; height:130px; position:absolute; top:75px; right: -50px; width:50px; cursor:pointer; }
  16. .jFlowPrev { background:url(\"/jscss/demoimg/201303/arrows.png\") no-repeat scroll -84px -35px transparent; height:130px; position:absolute; top:75px; left: -50px; width:50px; cursor:pointer; }
  17. .jFlowPrev:hover { background-position: -84px -235px; }
  18. .jFlowNext:hover { background-position: 0px -235px; }
  19. #myController {position: absolute; top: 5px; right: 5px; }
  20. .jFlowControl { background:url(\"/jscss/demoimg/201303/bullet.png\") no-repeat scroll 0px 0px transparent; float: left; width: 15px; height: 15px;}
  21. .jFlowControl:hover { background-position: -32px 0px; cursor: pointer; }
  22. .jFlowSelected, .jFlowSelected:hover {background-position: -15px 0px; }
  23. </style>
  24. <script type=\"text/javascript\" src=\"/ajaxjs/jquery-1.7.2.min.js\"></script>
  25. <script type=\"text/javascript\">
  26. (function ($) {
  27.     $.fn.jFlow = function (options) {
  28.         var opts = $.extend({}, $.fn.jFlow.defaults, options);
  29.         var randNum = Math.floor(Math.random() * 11);
  30.         var jFC = opts.controller;
  31.         var jFS = opts.slideWrapper;
  32.         var jSel = opts.selectedWrapper;
  33.         var cur = 0;
  34.         var timer;
  35.         var maxi = $(jFC).length;
  36.         var width = $(opts.slides).children().outerWidth();
  37.         var slidesNum = $(opts.slides).children().size();
  38.         var totalWidth = width * slidesNum;
  39.         var lastSlide = (slidesNum - 1);
  40.         var additionalSlide = slidesNum + 1;
  41.         var pause = opts.pause;
  42.         var slide = function (dur, i) {
  43.                 $(opts.slides).children().css({
  44.                     overflow: \"hidden\"
  45.                 });
  46.                 $(opts.slides + \" iframe\").hide().addClass(\"temp_hide\");            
  47.                 if (opts.effect == \"rewind\") {
  48.                     $(opts.slides).animate({
  49.                         marginLeft: \"-\" + (i * $(opts.slides).find(\":first-child\").width() + \"px\")
  50.                     }, opts.duration * (dur), opts.easing, function () {
  51.                         $(opts.slides).children().css({
  52.                             overflow: \"hidden\"
  53.                         });
  54.                         $(\".temp_hide\").show();
  55.                     });
  56.                 }
  57.                 else if (opts.effect == \"flow\") {
  58.                     if (i != 0) {
  59.                         $(\"#mySlides\").animate({
  60.                             left: -(i * width)
  61.                         }, function () {
  62.                             //if we are on the last slide then move the first slide into position
  63.                             if ((slidesNum - 1) == i) {
  64.                                 $(\".jFlowSlideContainer\").first().css({
  65.                                     left: (width * slidesNum)
  66.                                 });
  67.                             }
  68.                         });
  69.                         
  70.                         //fire off the paging function
  71.                         movePagingClass();
  72.                         
  73.                     }
  74.                     else {
  75.                     
  76.                         //animate the first slide container into position
  77.                         $(\".jFlowSlideContainer\").first().animate({
  78.                             //left slide distance is equal to width of slide times number of slides        
  79.                             left: (width * slidesNum)
  80.                        });
  81.                         $(opts.slides).css({
  82.                             // width is equal to the outer width of the container plus width one slide
  83.                             width: (width * additionalSlide)
  84.                          
  85.                         });
  86.                         $(opts.slides).animate({
  87.                             left: \"-\"+totalWidth
  88.                         },
  89.                         function () {
  90.                             //position the slide container element back to start
  91.                             $(this).css(\'left\', \'0px\');
  92.                             //position the first slide back to start
  93.                             $(\".jFlowSlideContainer\").first().css(\'left\', \'0px\');
  94.                      });
  95.                         $(\'.jFlowSelected\').removeClass(\'jFlowSelected\');
  96.                         $(jFC).first().addClass(\'jFlowSelected\');
  97.                     }
  98.                 }
  99.             }
  100.         var movePagingClass = function() {
  101.                 //advance the \'selected\' pagination class
  102.                 var thisController = $(\'.jFlowSelected\');
  103.                 $(thisController).removeClass(\'jFlowSelected\');
  104.                 $(thisController).next().addClass(\'jFlowSelected\');        
  105.         }
  106.         $(this).find(jFC).each(function (i) {
  107.             $(jFC).first().click(function() {
  108.                 $(\'.jFlowSlideContainer\').first().css({
  109.                     left: \'0px\'
  110.                 });
  111.                 $(\'#mySlides\').animate({
  112.                     left: \'0px\'
  113.                 });
  114.                  $(jFC).removeClass(jSel);
  115.                  $(this).addClass(jSel);
  116.             });
  117.             $(this).click(function () {
  118.                 dotimer();
  119.                 if ($(opts.slides).is(\":not(:animated)\")) {
  120.                     //slide to the page clicked
  121.                     var dur = Math.abs(cur - i);
  122.                     slide(dur, i);
  123.                     cur = i;
  124.                     $(jFC).removeClass(jSel);
  125.                     $(this).addClass(jSel);
  126.                 }
  127.             });
  128.         });        

  129.         $(opts.slides).before(\'
    + jFS.substring(1, jFS.length) + \'\">
    \'
    ).appendTo(jFS);
  130.         $(opts.slides).find(\".slide\").each(function () {
  131.             $(this).before(\'
    \'
    ).appendTo($(this).prev());
  132.         });
  133.         $(jFC).eq(cur).addClass(jSel);
  134.         var resize = function (x) {
  135.                 $(jFS).css({
  136.                     position: \"relative\",
  137.                     width: opts.width,
  138.                     height: opts.height,
  139.                     overflow: \"hidden\"
  140.                 });
  141.                 $(opts.slides).css({
  142.                     position: \"relative\",
  143.                     width: $(jFS).width() * $(jFC).length + \"px\",
  144.                     height: $(jFS).height() + \"px\",
  145.                     overflow: \"hidden\"
  146.                 });
  147.                 $(opts.slides).children().css({
  148.                     position: \"relative\",
  149.                     width: $(jFS).width() + \"px\",
  150.                     height: $(jFS).height() + \"px\",
  151.                     \"float\": \"left\",
  152.                     overflow: \"hidden\"
  153.                 });
  154.                 $(opts.slides).css({
  155.                     marginLeft: \"-\" + (cur * $(opts.slides).find(\":eq(0)\").width() + \"px\")
  156.                 });
  157.             }
  158.             resize();
  159.         $(window).resize(function () {
  160.             resize();
  161.         });
  162.         $(opts.prev).click(function () {
  163.             dotimer();
  164.             doprev();
  165.         });
  166.         $(opts.next).click(function () {
  167.             dotimer();
  168.             donext();
  169.         });
  170.         var doprev = function (x) {
  171.                 if (opts.effect == \'rewind\') {
  172.                     if ($(opts.slides).is(\":not(:animated)\")) {
  173.                         var dur = 1;
  174.                         if (cur > 0) {
  175.                             cur--;
  176.                         } else {
  177.                             cur = maxi - 1;
  178.                             dur = cur;
  179.                         }
  180.                         $(jFC).removeClass(jSel);
  181.                         slide(dur, cur);
  182.                         $(jFC).eq(cur).addClass(jSel);
  183.                     }
  184.                 }
  185.                 else if (opts.effect == \'flow\') {
  186.                     if(cur == 0) {
  187.                      $(opts.slides).css({
  188.                             width: (width * additionalSlide)
  189.                         });
  190.                                         
  191.                         $(\'.jFlowSlideContainer:last\').css({
  192.                             position: \"absolute\",
  193.                             left: \"-\"+width+\"px\"
  194.                         });
  195.                         $(\'#mySlides\').animate({
  196.                             paddingLeft: width+\"px\"
  197.                         });
  198.                         $(\'.jFlowSlideContainer:last\').animate({
  199.                             left: \"0px\"
  200.                         }, function () {
  201.                             $(this).css({
  202.                                 position: \"relative\",
  203.                                 left: \"\"
  204.                             });
  205.                             $(\'#mySlides\').css({
  206.                                 padding: \"0px\",
  207.                                 left: \"-\"+(totalWidth - width)+\"px\"
  208.                             });
  209.                         
  210.                         });
  211.                          cur = slidesNum - 1;
  212.                         $(\'.jFlowSelected\').removeClass(\'jFlowSelected\');
  213.                         $(jFC).last().addClass(\'jFlowSelected\');
  214.                         
  215.                     } //else slide as normal
  216.                     else {
  217.                         if($(opts.slides).find(\":first-child\").css({ left: totalWidth })) {
  218.                             $(opts.slides).find(\":first-child\").css(\'left\',\'0px\');
  219.                         }
  220.                         $(\'#mySlides\').animate({
  221.                             left: -(width * (cur - 1))
  222.                         }, {
  223.                             queue: false
  224.                         });
  225.                         cur = cur - 1;
  226.                         var thisController = $(\'.jFlowSelected\');
  227.                         $(thisController).removeClass(\'jFlowSelected\');
  228.                         $(thisController).prev().addClass(\'jFlowSelected\');    
  229.                     }
  230.                 } else {
  231.                     alert(\'Error: No effect entered as option!\');
  232.                 }
  233.             }
  234.         var donext = function (x) {
  235.             if(cur == lastSlide) {
  236.                     $(\".jFlowSlideContainer\").first().css({
  237.                         \"left\": totalWidth+\"px\"
  238.                         });
  239.                 }
  240.              if (opts.effect == \'rewind\') {
  241.                     if ($(opts.slides).is(\":not(:animated)\")) {
  242.                         var dur = 1;
  243.                         if (cur < maxi - 1) cur++;
  244.                         else {
  245.                             cur = 0;
  246.                             dur = maxi - 1;
  247.                         }
  248.                         $(jFC).removeClass(jSel);
  249.                         //$(jFS).fadeOut(\"fast\");
  250.                         slide(dur, cur);
  251.                         //$(jFS).fadeIn(\"fast\");
  252.                         $(jFC).eq(cur).addClass(jSel);
  253.                     }
  254.                 }
  255.                 else if (opts.effect == \'flow\') {
  256.                     //check to see which slide we are on and set \"cur\" variable
  257.                     if ($(opts.slides).is(\":not(:animated)\")) {
  258.                         var dur = 1;
  259.                         if (cur < maxi - 1) cur++;
  260.                         else {
  261.                             cur = 0;
  262.                             dur = maxi - 1;
  263.                         }
  264.                     }
  265.                     slide(dur, cur);
  266.                 } //end: \"flow\'
  267.                 $(\"#della\").html(cur);
  268.             } //end: donext function
  269.             
  270.         //the jFlow Timer
  271.         var dotimer = function (x) {
  272.                 if ((opts.auto) == true) {
  273.                     if (timer != null) clearInterval(timer);
  274.                     timer = setInterval(function () {
  275.                         $(opts.next).click();
  276.                     }, pause);
  277.                 }
  278.             }
  279.             dotimer();
  280.         $(opts.slides).hover(
  281.         function () {
  282.             clearInterval(timer);
  283.         }, function () {
  284.             dotimer();
  285.         });
  286.     };
  287.     $.fn.jFlow.defaults = {
  288.         controller: \".jFlowControl\",
  289.         slideWrapper: \"#jFlowSlider\",
  290.         slides: \"#mySlides\",
  291.         selectedWrapper: \"jFlowSelected\",
  292.         effect: \"flow\",
  293.         width: \"800px\",
  294.         height: \"350px\",
  295.         duration: 400,
  296.         pause: 5000,
  297.         prev: \".jFlowPrev\",
  298.         next: \".jFlowNext\",
  299.         auto: true
  300.     };
  301. })(jQuery);




  302. ">
  303.   
    ">
  304.     
    ">
  305.       
    " class=\"slide\"> /jscss/demoimg/201303/jflow-sample-slide1.jpg\" alt=\"Slide 1 jFlow Plus\" />
  306.         
    ">
  307.           

    You Asked, jFlow Delivered


  308.           

    It\'s all about the Community and giving back. To keep with this tradition, jFlow Plus now has more of the features you want.


  309.         

  •       

  •       
    " class=\"slide\"> /jscss/demoimg/201303/jflow-sample-slide2.jpg\" alt=\"Slide 2 jFlow Plus\" />
  •         
    ">
  •           

    W3C Valid


  •           

    Are you a stickler for writing valid code? So is jFlow. Run this puppy through W3C\'s validator to see it pass the test!


  •         

  •       

  •       
    " class=\"slide\"> /jscss/demoimg/201303/jflow-sample-slide3.jpg\" alt=\"Slide 3 jFlow Plus\" />
  •         
    ">
  •           

    Frequent Code Updates


  •           

    This slider is actively developed and used by thousands of websites. More features coming soon including more effects and options.


  •         

  •       

  •       
    " class=\"slide\"> /jscss/demoimg/201303/jflow-sample-slide4.jpg\" alt=\"Slide 3 jFlow Plus\" />
  •         
    ">
  •           

    Notice the Slide Navigation?


  •           

    That\'s a new feature. Click on the paging buttons in the top-right to quickly jump to any jFlow slide number.


  •         

  •       

  •     
  •     
    "> jFlowControl\"> jFlowControl\"> jFlowControl\"> jFlowControl\">

  •     
    ">

  •     
    ">

  •   
  •  
  • <!--end: container -->


  • 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29737856/viewspace-1239953/ ,如需转载,请注明出处,否则将追究法律责任。

    请登录后发表评论 登录
    全部评论

    注册时间:2014-07-04

    • 博文量
      95
    • 访问量
      122213