您现在的位置是:首页 > 网站制作 > WordpressWordpress
WordPress实现点击展开折叠二级菜单+当前点击后变色
杰帅2023-07-20【Wordpress】人已围观
简介一个客户需要开发这样的网站,左侧菜单是一个点击展开式菜单,默认是收缩的,点击后会展开子菜单。当用户点击某个子菜单时,当前点击的菜单会变成红色。效果如下图:
一个客户需要开发这样的网站,左侧菜单是一个点击展开式菜单,默认是收缩的,点击后会展开子菜单。当用户点击某个子菜单时,当前点击的菜单会变成红色。效果如下图:
功能要求:
- 一级菜单下面有多个子菜单;
- PC端点击目录的文章后目录树会自动关闭;
- PC端点击目录中的文章,设置一个颜色,用来区分当前正在阅读的文章;
以下是通过WORDPRESS程序来实现这样的功能,如果你在做网站时,也需要这样的功能的话,可以使用以下的代码。
PHP代码
<div class="sidebarleft">
<ul class="cd-accordion-menu animated">
<li class="children">
<input type="checkbox" name ="group-1" id="group-1" checked>
<label for="group-1"> pyton教程</label>
<ul>
<?php
$catidall=Array("31","32","33","34","35");
for ($i=0;$i<count($catidall);$i++) {?>
<li class="cat-item cat-item-<?php echo $catidall[$i];?>">
<input type="checkbox" name ="sub-group-level-<?php echo $catidall[$i];?>" id="sub-group-level-<?php echo $catidall[$i];?>">
<label for="sub-group-level-<?php echo $catidall[$i];?>"><?php echo get_cat_name($catidall[$i])?></label>
<?php $singleurl = get_permalink($post_id);?>
<?php foreach((get_the_category()) as $category);?>
<ul class="children"<?php if(is_single()&&$category->cat_ID==$catidall[$i]){?> style="display:block;"<?php }?>>
<?php query_posts('cat='.$catidall[$i].'' . $mcatID. '&caller_get_posts=1&showposts=100&order=ASC'); ?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"<?php if(get_permalink($post_id)==$singleurl){?> style="color:red"<?php }?>><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</li>
<?php } ?>
</ul>
</li>
</ul>
</div>
<script src="<?php bloginfo('template_directory'); ?>/jquerysfq/js/jquery-2.1.1.min.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/jquerysfq/js/main.js"></script>
JS文件(main.js)
jQuery(document).ready(function(){
var accordionsMenu = $('.cd-accordion-menu');
if( accordionsMenu.length > 0 ) {
accordionsMenu.each(function(){
var accordion = $(this);
//detect change in the input[type="checkbox"] value
accordion.on('change', 'input[type="checkbox"]', function(){
var checkbox = $(this);
console.log(checkbox.prop('checked'));
( checkbox.prop('checked') ) ? checkbox.siblings('ul').attr('style', 'display:none;').slideDown(300) : checkbox.siblings('ul').attr('style', 'display:block;').slideUp(300);
});
});
}
});
CSS代码
/* --------------------------------
Primary style
-------------------------------- */
*, *::after, *::before {
box-sizing: border-box;
}
ul,ol{list-style:none;}
/* --------------------------------
Main Components
-------------------------------- */
label { font-weight: 400;}
.sidebarleft .children li{ overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;}
.sidebarleft{padding-right:5px;overflow: hidden;font-size:14px;}
ul,ol{padding:0;}
.cat-item-none{display:none;}
.cd-accordion-menu {
width: 100%;
padding:0;
margin: 2em auto;
}
.cd-accordion-menu ul {
/* by default hide all sub menus */
display: none;
}
.cd-accordion-menu li {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.cd-accordion-menu input[type=checkbox] {
/* hide native checkbox */
position: absolute;
opacity: 0;
}
.cd-accordion-menu label, .cd-accordion-menu a {
position: relative;
display: block;
background: #FAFAFA;
color: #000000;
font-size: 1em;
}
.no-touch .cd-accordion-menu label:hover, .no-touch .cd-accordion-menu a:hover {
background: #52565d;
}
.cd-accordion-menu label::before, .cd-accordion-menu label::after, .cd-accordion-menu a::after {
/* icons */
content: '';
display: inline-block;
width: 16px;
height: 16px;
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
.cd-accordion-menu label {
cursor: pointer;
}
.cd-accordion-menu label::before, .cd-accordion-menu label::after {
background-image: url(../img/cd-icons.svg);
background-repeat: no-repeat;
}
.cd-accordion-menu label::before {
/* arrow icon */
left: 18px;
background-position: 0 0;
-webkit-transform: translateY(-50%) rotate(-90deg);
-moz-transform: translateY(-50%) rotate(-90deg);
-ms-transform: translateY(-50%) rotate(-90deg);
-o-transform: translateY(-50%) rotate(-90deg);
transform: translateY(-50%) rotate(-90deg);
}
.cd-accordion-menu label::after {
/* folder icons */
left: 41px;
background-position: -16px 0;
}
.cd-accordion-menu input[type=checkbox]:checked + label::before {
/* rotate arrow */
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);
}
.cd-accordion-menu input[type=checkbox]:checked + label::after {
/* show open folder icon if item is checked */
background-position: -32px 0;
}
.cd-accordion-menu input[type=checkbox]:checked + label + ul,
.cd-accordion-menu input[type=checkbox]:checked + label:nth-of-type(n) + ul {
/* use label:nth-of-type(n) to fix a bug on safari (<= 8.0.8) with multiple adjacent-sibling selectors*/
/* show children when item is checked */
display: block;
}
.cd-accordion-menu ul label/*,
.cd-accordion-menu ul a */{
box-shadow: inset 0 -1px #ddd;
padding-left: 67px;
}
.no-touch .cd-accordion-menu ul label:hover, .no-touch
.cd-accordion-menu ul a:hover {
background: #3c3f45;
}
.cd-accordion-menu > li:last-of-type > label,
.cd-accordion-menu > li:last-of-type > a,
.cd-accordion-menu > li > ul > li:last-of-type label,
.cd-accordion-menu > li > ul > li:last-of-type a {
box-shadow: none;
}
.cd-accordion-menu ul label::after,
.cd-accordion-menu ul a::after {
left: 44px;
}
.cd-accordion-menu ul ul label,
.cd-accordion-menu ul ul a {
padding-left: 50px;
}
.cd-accordion-menu ul ul label::before {
left: 54px;
}
.cd-accordion-menu ul ul label::after,
.cd-accordion-menu ul ul a::after {
left: 77px;
}
.cd-accordion-menu ul ul ul label,
.cd-accordion-menu ul ul ul a {
padding-left: 118px;
}
.cd-accordion-menu ul ul ul label::before {
left: 72px;
}
.cd-accordion-menu ul ul ul label::after,
.cd-accordion-menu ul ul ul a::after {
left: 95px;
}
.cd-accordion-menu label {
padding: 10px 10px 10px 45px;
font-size: 1em;
}
.cd-accordion-menu a {
padding: 5px 10px 5px 45px;
font-size: 1em;
color:#444;
background:url(/wp-content/uploads/2019/07/ico1.png) no-repeat 30px 50%;
}
.cd-accordion-menu label::before {
left: 5px;
}
.cd-accordion-menu label::after {
left: 26px;
}
.cd-accordion-menu ul label::before {
left: 24px;
}
.cd-accordion-menu ul ul label::before {
left: 72px;
}
.cd-accordion-menu ul ul label::after,
.cd-accordion-menu ul ul a::after {
left: 101px;
}
.cd-accordion-menu ul ul ul label,
.cd-accordion-menu ul ul ul a {
padding-left: 154px;
}
.cd-accordion-menu ul ul ul label::before {
left: 96px;
}
.cd-accordion-menu ul ul ul label::after,
.cd-accordion-menu ul ul ul a::after {
left: 125px;
}
.cd-accordion-menu.animated label::before {
/* this class is used if you're using jquery to animate the accordion */
-webkit-transition: -webkit-transform 0.3s;
-moz-transition: -moz-transform 0.3s;
transition: transform 0.3s;
}
Tags:wordpress主题制作 wordpress教程 二级菜单
很赞哦! ()
相关文章
- 如何制作wordpress 网站搜索功能| WordPress教程网
- WordPress主题制作教程常用的函数调用举例
- WordPress主题制作调用作者信息的17个函数
- WordPress主题制作全过程(十一):制作page.php
- WordPress主题制作全过程(十):制作comments.php
- WordPress主题制作全过程(九):制作single.php
- WordPress主题制作全过程(八):制作index.php
- WordPress主题制作全过程(七):制作sidebar.php
- WordPress主题制作全过程(六):制作footer.php
- WordPress主题制作全过程(五):制作header.php
随机图文
-
wordpress 上传的图片不显示的问题 base64,data:image/gif
-
wp_reset_postdata 和 wp_reset_query 的作用与区别
什么时候使用wp_reset_query,什么时候用wp_reset_postdata? -
wordpress发布文章HTML标签被自动过滤掉该如何处理?
wordpress发布文章时很多html标签都会自动过滤掉,造成了文章中无法添加<style></style><script></script>等标签。那么该如何如何处理呢? 解决方法一: 将wp-includes文件夹下 -
WordPress 页面模板(Page Template)下拉列表不显示的原因及解决方法
WordPress 的自定义页面模板是一个非常强大好用的功能,使用它新建一些静态页面(Page),添加上一些数据调用的函数,再在网页上做一个导航连接到对应的页面就可以实现很多自定义的功
文章评论
本站推荐
标签云
猜你喜欢
- 提高WordPress的访问速度
- 安装Elementor Pro插件后提示Warning: Undefined array key “requires”的解决办法
- The7主题11.7.2完美汉化中文版|强大多功能自定义设计WordPress企业主题包含模板 - 搬主题
- 纯代码增强WordPress文章编辑器功能按钮
- WordPress登陆用户和普通访客显示不同菜单设置图文教程
- 为你解析12种网站降权原因
- ION主机免费WordPress控制台无法登陆的解决方案
- WordPress文章页实现所属分类高亮显示
- WordPress网站故障解决技巧:专家分享实用经验
- WordPress官网打不开429 Too Many Requests的解决方法