jquery - 使用 Data Cycle FX 的 jQuery 旋转木马菜单内的中心事件 li 元素

标签 jquery css wordpress infinite-carousel

我已经通读了许多例子,但似乎没有一个能解决我所有的问题。我有一个 Wordpress 生成的菜单,其中 LI 元素包含指向由 Data Cycle FX 插件和 jQuery 控制的内容的 anchor 链接。一切都按照我的意愿工作,除了在页面刷新/加载时,我想检测哪个 LI 元素是 .active,并让该元素在可见区域内居中。 (如果重新加载页面,这主要是一个问题,因为任何不是前 6 个元素之一的 li.active 都被隐藏了。)我在想我需要检测 .sub-navigation div 的宽度并以某种方式使用position() 或 offset() ...但我的尝试一直没有成功。非常感谢任何建议:-)。

这是我的显示菜单结构的 HTML:

 <body>
 <div class="sub-navigation">
 <div class="prev">
 <a href="#">&lt;</a>
 </div><!--end prev-->
 <div class="next">
 <a href="#">&gt;</a>
 </div><!--end next-->

 <div class="menu-product-pages-container">
 <ul class="subnav cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout=0 data-cycle-easing="linear" data-cycle-carousel-offset="10"data-cycle-next=".next a" data-cycle-prev=".prev a" data-cycle-slides="li">
 <li class="refrigeration-menu"><a href="#refrigeration">Refrigeration</a></li>
 <li class="food-prep-menu"><a href="#food-prep">Food Prep</a></li>
 <li class="cooking-menu"><a href="#cooking">Cooking</a></li>
 <li class="baking-menu"><a href="#baking">baking</a></li>
 <li class="warewashing-menu"><a href="#warewashing">Warewashing</a></li>
 <li class="storage-menu"><a href="#storage">Storage</a></li>
 <li class="food-transport-menu"><a href="#food-transport">Food Transport</a></li>
 <li class="ventilation-menu"><a href="#ventilation">Ventilation</a></li>
 <li class="serving-lines-menu"><a href="#serving-lines">Serving Lines</a></li>
 <li class="conveyors-menu"><a href="#conveyors">Conveyors</a></li>
 </ul></div></div><!--.sub-navigation-->
 </body> 

这是我的 CSS:

.sub-navigation {
width: 100%;
float: left;
}
.subnav {
background: #777;
list-style-type: none;
margin: 0px;
padding-left: 0px;
position: relative;
height: 40px;
overflow-x: hidden;
white-space: nowrap;
}
.subnav li {
display: inline;
line-height: 40px;
padding:  0 30px 0 30px;
}
.subnav li a {
color: #000;
display: block;
font-size: 95%;
text-transform:uppercase;
padding-left: 15px;
}
.active {
background-color: #c32d2e;
}
.prev {
color: #ccc;
float: left;
font-size: 185%;
font-weight: bold;
line-height: 40px;
width: 2%;
height: 39px;
}
.prev a, .next a {
color: #ccc;
display: block;
margin: 5px auto 0 auto;
text-align: center;
text-decoration: none;
width: 98%;
}
.next {
float: right;
font-size: 100%;
font-weight: bold;
line-height: 40px;
width: 2%;
height: 39px;
}

我的查询:

 $('ul.subnav').each(function(){

 var $active, $content, $links = $(this).find('a');
 $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
 $active.parent().addClass('active');
 $content = $($active.attr('href'));  

 $links.not($active).each(function () {
 $($(this).attr('href')).hide();
});
 $("html, body").animate({ scrollTop: 0 }, "fast");
// Bind the click event handler
 $(this).on('click', 'a', function(e){
 $("html, body").animate({ scrollTop: 0 }, "fast");
// Make the old tab inactive.
 $active.parent().siblings().removeClass('active');
 $active.parent().removeClass('active');
 $content.hide();
// Update the variables with the new link and content
 $active = $(this);
 $content = $($(this).attr('href'));
// Make the tab active  
 $active.parent().addClass('active');
 $content.show();
//change url in bar
 window.history.pushState($active);
// Prevent the anchor's default click action
 e.preventDefault();
});
});

似乎与此类似的函数应该可以使 .active 元素在视口(viewport)中居中,但我的理解/方法一定有问题:

function scrollMenu(){
$viewportWidth = $('.subnav').width(),
$activeTab = $('ul').find('li.active'),
$elWidth = $activeTab.width(),
$elOffset = $activeTab.offset();
$('.active').parent().scrollTo(elOffset.left - (elWidth/2) - (viewportWidth/2));
}

最佳答案

你可以使用 hasClass 方法,例如。

$('li').hasClass('active') // true|false

如果你想一次选中它,你可以使用:

$('li.active')

关于jquery - 使用 Data Cycle FX 的 jQuery 旋转木马菜单内的中心事件 li 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996155/

相关文章:

JavaScript 模块模式对比jQuery 插件

ios - 从 uiwebview 的应用程序中加载 .CSS 文件

mysql - 如何移动 Wordpress 站点并搜索和替换数据库而不出现内部服务器错误?

php - 在 WooCommerce 中为自定义字段使用 wc_get_orders() orderby 参数

CSS 属性选择器 : how to escape newlines within attribute value?

html - 样式不适用于 iframe 内容

javascript - 为什么 JS/Jquery 在 IE7 中似乎不起作用

javascript - 如何在 HTML 表单中捕获键盘输入

jquery - ajax 中的成功函数不起作用

html - iOS 7.1 最小用户界面导致视口(viewport)高度 css 单元 (vh) 出现问题