【问题标题】:Trying to create scrolling horizontal thumbnail navigation that hides on left side when not in use尝试创建在不使用时隐藏在左侧的滚动水平缩略图导航
【发布时间】:2011-03-05 03:28:19
【问题描述】:

我正在尝试按照以下教程中的说明重新创建以下类型的滚动缩略图导航:

http://tympanus.net/codrops/2010/05/10/scrollable-thumbs-menu/

我需要我的拇指从左侧水平滑出。我已经尽我所能修改了代码,但我无法让它工作。 (认为​​问题出在 jquery 的前三分之一处)。

这是我要约会的:

HTML

<ul class="menu" id="menu">
    <li>
        <a href="#"></a>
        <div class="sc_menu_wrapper">
            <div class="sc_menu">
                <a href="#"><img src="images/gallery/1.jpg" alt=""/></a>
                <a href="#"><img src="images/gallery/2.jpg" alt=""/></a>
                <a href="#"><img src="images/gallery/3.jpg" alt=""/></a>
                <a href="#"><img src="images/gallery/4.jpg" alt=""/></a>
                <a href="#"><img src="images/gallery/5.jpg" alt=""/></a>
            </div>
        </div>
    </li>
    </ul>

CSS

/* Navigation Style */
ul.menu{
position: fixed;
top: 0px; 
left:0px;
width: 100%;
}

ul.menu li{
position:relative;
width: 100% 
}

ul.menu li > a{
position:absolute;
top:300px;
left:0px;
width:40px;
height:200px;
background-color:#e7e7e8;
}

/* Thumb Scrolling Style */

div.sc_menu_wrapper {
position: absolute;
right: 0px; 
height: 220px;
overflow: hidden;
top: 300px; 
left:0px;
visibility:hidden;
}

div.sc_menu {
height: 200px; 
visibility:hidden;
}

.sc_menu a {
display: block;
background-color:#e7e7e8;
}
.sc_menu img {
display: block;
border: none;
opacity:0.3;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=30);
}

JQUERY

  $(function(){

      // function to make the thumbs menu scrollable 
            function buildThumbs($elem){
                var $wrapper        = $elem.next();
                var $menu       = $wrapper.find('.sc_menu');
                var inactiveMargin  = 220;

                // move the scroll down to the beggining (move as much as the height of the menu)

                $wrapper.scrollRight($menu.outerHeight());

               //when moving the mouse up or down, the wrapper moves (scrolls) up or down 

                $wrapper.bind('mousemove',function(e){

                    var wrapperWidth    = $wrapper.width();
                    var menuWidth   = $menu.outerWidth() + 2 * inactiveMargin;
                    var top     = (e.pageX - $wrapper.offset().right) * (menuWidth - wrapperWidth) / wrapperWidth - inactiveMargin;

                   $wrapper.scrollRight(right+10);
                });
            }

      var stacktime;

      $('#menu li > a').bind('mouseover',function () {
          var $this = $(this);

          buildThumbs($this);

          var pos   =   $this.next().find('a').size();
          var f =   function(){
              if(pos==0) clearTimeout(stacktime);
              $this.next().find('a:nth-child('+pos+')').css('visibility','visible');
              --pos;
          };

          // each thumb will appear with a delay 
          stacktime = setInterval(f , 50);
      });


      /// on mouseleave of the whole <li> the scrollable area is hidden 
      $('#menu li').bind('mouseleave',function () {
          var $this = $(this);
          clearTimeout(stacktime);
          $this.find('.sc_menu').css('visibility','hidden').find('a').css('visibility','hidden');
          $this.find('.sc_menu_wrapper').css('visibility','hidden');
      });

      // when hovering a thumb, change its opacity 
      $('.sc_menu a').hover(
      function () {
          var $this = $(this);
          $this.find('img')
          .stop()
          .animate({'opacity':'1.0'},400);
      },
      function () {
          var $this = $(this);
          $this.find('img')
          .stop()
          .animate({'opacity':'0.3'},400);
      }
  );
  });

想知道是否有鹰眼能够指出我哪里出错了。由于我对 JQuery 的了解有限,我猜它就在那里。

我非常感谢任何人抽出时间仔细查看。

谢谢!

【问题讨论】:

    标签: jquery navigation scroll gallery thumbnails


    【解决方案1】:

    我给你发了demo:)

    我无法让您的代码与演示一起使用,但首先需要更改的是所有权限。没有scrollRight 这样的东西。这只是代码中修复了这些问题的第一个函数。

      // function to make the thumbs menu scrollable 
            function buildThumbs($elem){
                var $wrapper       = $elem.next();
                var $menu          = $wrapper.find('.sc_menu');
                var inactiveMargin = 220;
    
                // move the scroll down to the beggining (move as much as the height of the menu)
    
                $wrapper.scrollLeft($menu.outerHeight());
    
               //when moving the mouse up or down, the wrapper moves (scrolls) up or down 
    
                $wrapper.bind('mousemove',function(e){
    
                    var wrapperWidth = $wrapper.width();
                    var menuWidth    = $menu.outerWidth() + 2 * inactiveMargin;
                    var left         = (e.pageX - $wrapper.offset().left) * (menuWidth - wrapperWidth) / wrapperWidth - inactiveMargin;
    
                   $wrapper.scrollLeft(left+10);
                });
            }
    

    哦,在这部分 CSS 中添加 float:left:

    .sc_menu img {
     display: block;
     border: none;
     float: left;
     opacity:0.3;
     filter:progid:DXImageTransform.Microsoft.Alpha(opacity=30);
    }
    

    已更新以使滚动和突出显示正常工作 - 这可能不是最有效的方法,但我没有对原始代码进行太多更改。 Updated demo here.

    CSS

    /* Navigation Style */
    ul.menu{
     position: fixed;
     top: 0px;
     left:0px;
     width: 100%;
    }
    
    ul.menu li{
     position:relative;
     width: 100%
    }
    
    ul.menu li > a {
     display: block;
     position:absolute;
     top:300px;
     left:0px;
     width:40px;
     height:200px;
     background-color:#e7e7e8;
    }
    
    /* Thumb Scrolling Style */
    
    div.sc_menu_wrapper {
     position: relative;
     height: 220px;
     overflow: hidden;
     top: 300px;
     left: 0px;
     visibility:hidden;
    }
    
    div.sc_menu {
     height: 195px;
     visibility:hidden;
     overflow: hidden;
     position: absolute;
     top: 0;
     left: 0;
     display: block;
     top: 0;
     left: 0;
     width: 100%;
    }
    
    .sc_menu a {
     background-color:#e7e7e8;
    }
    .sc_menu img {
     height: 195px;
     width: 130px;
     float: left;
     display: block;
     border: none;
     opacity:0.3;
     filter:progid:DXImageTransform.Microsoft.Alpha(opacity=30);
    }
    

    脚本

    $(function(){
     // function to make the thumbs menu scrollable
     function buildThumbs($elem) {
      var $wrapper = $elem.next();
      var $menu = $wrapper.find('.sc_menu');
      var leftOffset = $wrapper.offset().left;
    
      // move the scroll left to the beggining
      $wrapper.scrollLeft(0);
      // make menuWidth (width of all images side by side) include the offset
      var menuWidth = leftOffset;
      // calculate the width of the menu by adding each image width (includes any padding, border & margin)
      $menu.find('img').each(function(){
       $(this).css('left', menuWidth );
       menuWidth += $(this).outerWidth(true);
      });
      // set calculated menu width - if not done, the images will wrap to the next line
      $menu.width(menuWidth);
    
      //when moving the mouse left or right, the wrapper moves (scrolls) left or right
      $wrapper.bind('mousemove', function(e){
       var wrapperWidth = $wrapper.outerWidth(true);
       var left = ( e.pageX - leftOffset ) * (menuWidth - wrapperWidth) / wrapperWidth;
       $wrapper.scrollLeft(left);
      });
     }
    
     var stacktime;
     $('#menu li > a').bind('mouseover', function(){
      var $this = $(this);
      // set up thumbnail scrolling
      buildThumbs($this);
      var pos = 0,
          len = $this.next().find('a').length;
      var f = function () {
       if (pos > len) { clearTimeout(stacktime); }
       $this.next().find('a:nth-child(' + pos + ')').css('visibility', 'visible');
       pos++;
      };
      // each thumb will appear with a delay
      stacktime = setInterval(f, 50);
     });
    
     // on mouseleave, the whole the scrollable area is hidden
     $('#menu li').bind('mouseleave', function(){
      var $this = $(this);
      clearTimeout(stacktime);
      $this.find('.sc_menu').css('visibility', 'hidden').find('a').css('visibility', 'hidden');
      $this.find('.sc_menu_wrapper').css('visibility', 'hidden');
     });
    
     // when hovering a thumb, change its opacity
     $('.sc_menu a').hover(function(){
      $(this).find('img').stop().animate({ 'opacity': '1.0' }, 400);
     }, function () {
      $(this).find('img').stop().animate({ 'opacity': '0.3' }, 400);
     });
    });
    

    【讨论】:

    • 哦,天哪,非常非常感谢您抽出宝贵时间帮助我解决这个问题。 (我无法相信这个社区中人们的慷慨)。我今晚将在我的网站上工作,如果/当我让它运行时,我会回来给你应有的荣誉(复选标记和箭头)。那个 jsfiddle 网站看起来很棒。非常感谢您分享链接。我显然必须研究这个资源。我可以亲吻你湿漉漉的小鼻子,Fudgey :) 再次感谢你。
    • 嗨,Fudgey,我试过了,效果很好……从右侧出来,向左打开。我知道你已经在这方面做了很多工作——如果你能提供一些关于我应该使用哪个 JQuery CSS 定位选择器的建议,以便让它从左侧打开,朝向右侧(希望这是有道理的?)- 这是这个项目所需要的。是 $(selector).offset() 吗?我以前从未使用过这些选择器。不管怎样,我会继续在这里工作,如果我成功了,我会发布我的结果。
    • 嗨,我让图像从左侧弹出,但后来我意识到它不像教程上下滚动那样左右滚动...给我多一点时间继续努力,我会更新上面的帖子:)
    • 好的,我已经更新了演示 (jsfiddle.net/6eTHr/1),我希望这就是你想要的! :)
    • 你做到了!我简直不敢相信!我一直在研究它直到凌晨,但无法弄清楚。 (在那之前,我已经为此苦苦挣扎了将近一个星期)。但似乎您也为此工作了几个小时(但有结果!)。我怎么能感谢你呢?我将剖析您的代码以弄清楚您是如何使其工作的。这已经教会了我很多。对于您为我找到这个解决方案所付出的努力和工作,我非常感激和谦卑。你让我开心!我的月份!我再次真诚地感谢你,Fudgey。我希望能够及时支付。
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多