完美实现淘宝易迅左边菜单树,鼠标over事件显示菜单内容
来源:互联网 发布:巨人网络 刘伟 编辑:程序博客网 时间:2024/04/29 04:32
<s:iterator value="#attr.shoponelevellist" var="list"><div class="psitem"><h3 onmouseover="showsChild(this,${list.id});">${list.name}</h3><div class="item-list clear"><div class="subitem"><dl class="sort1 clear"><dd></dd></dl></div></div></div></s:iterator>
迭代后台取出的大分类的数据,显示给JSP页面,就是左边菜单中开始显示的分类,下面给出后台取出数据的代码。
List shoponelevellist = pointshopservice.findSolByid(shopnameid);session.put("shoponelevellist",shoponelevellist);
JS代码,异步从后台取出小分类的数据,显示出来。
function showsChild(t,id) {if($(t).next().find("dd").html().length<1) { $.getJSON("<%=path%>/PointShopAction!showChild", {pid:id}, function(data){var childsHtml = " ";for(var i=0;i<data.ShoponelevelList.length;i++) {childsHtml += "<em>"+ data.ShoponelevelList[i].name +"</em>";}$(t).next().find("dd").html(childsHtml) ;});}}
解释下if($(t).next().find("dd").html().length<1) ,这段代码实现当该大类的子类都被加载出来之后,鼠标放上去不会在走JS方法showsChild(this,${list.id}),也相当于不会向数据库中读取相同的数据,这样会减少对数据库的读取,否则鼠标随意在上面移动每次onmouseover将对数据库进行读取一次,将很大程度的加大对系统的压力和对数据库的承载,只需读取第一次,读取出来的数据将会保留在页面,可直接显示。
/** * 显示商品分类子类 */public void showChild() {List<Shoponelevel> list = service.findByPid(pid);try {Util.printResult(response, JsonUtil.ListToJson(list)); //把list数据封装成json数据} catch (IOException e) {e.printStackTrace();}}
/** * List集合转化为String */public static Object lListToJson(List List) {String jsonStr = null;StringBuffer strBuffer = new StringBuffer();strBuffer.append("{");strBuffer.append("\"ShoponelevelList\":[");for (int i = 0; i < ShoponelevelList.size(); i++) {Shoponelevel temp = ShoponelevelList.get(i);strBuffer.append("{");strBuffer.append("\"id\":\"");strBuffer.append(temp.getId());strBuffer.append("\",");strBuffer.append("\"name\":\"");strBuffer.append(temp.getName());strBuffer.append("\",");strBuffer.append("\"sortnum\":\"");strBuffer.append(temp.getSortnum());strBuffer.append("\"");if (i < ShoponelevelList.size() - 1) {strBuffer.append("},");} else {strBuffer.append("}");}}strBuffer.append("]"); strBuffer.append("}");jsonStr = strBuffer.toString();return jsonStr;}
样式显示JS
$('.catalog >.psitem-box> .psitem').hover(function(){var eq = $('.catalog > .psitem-box > .psitem').index(this),//获取当前滑过是第几个元素h = $('.catalog').offset().top,//获取当前下拉菜单距离窗口多少像素s = $(window).scrollTop(),//获取游览器滚动了多少高度i = $(this).offset().top,//当前元素滑过距离窗口多少像素item = $(this).children('.item-list').height(),//下拉菜单子类内容容器的高度sort = $('.catalog').height();//父类分类列表容器的高度if ( item < sort ){//如果子类的高度小于父类的高度if ( eq == 0 ){$(this).children('.item-list').css('top', (i-h));} else {$(this).children('.item-list').css('top', (i-h)+1);}} else {if ( s > h ) {//判断子类的显示位置,如果滚动的高度大于所有分类列表容器的高度if ( i-s > 0 ){//则 继续判断当前滑过容器的位置 是否有一半超出窗口一半在窗口内显示的Bug,$(this).children('.item-list').css('top', (s-h)+2 );} else {$(this).children('.item-list').css('top', (s-h)-(-(i-s))+2 );}} else {$(this).children('.item-list').css('top', 3 );}}$(this).addClass('hover');$(this).children('.item-list').css('display','block');},function(){$(this).removeClass('hover');$(this).children('.item-list').css('display','none');});
后期补效果图
0 0
- 完美实现淘宝易迅左边菜单树,鼠标over事件显示菜单内容
- 左边菜单,右边内容
- 左边菜单,右边内容
- 左边菜单,点击左边右边内容改变
- 鼠标事件---显示隐藏的菜单
- 横向滑动ViewGoup(左边菜单右边内容)效果的实现
- 点击隐藏显示左边菜单
- jquery鼠标移上显示隐藏菜单完美版
- 太平洋电脑网左边菜单的实现
- 让superfish的菜单在左边显示
- jquery - 左边栏菜单隐藏/显示
- jquery - 左边栏菜单滑动隐藏/显示
- 实现鼠标点击及键盘控制下拉菜单事件
- js屏蔽鼠标右键默认事件以实现自定义菜单
- 鼠标悬浮显示二级菜单
- DIV CSS实现左边商品分类菜单
- 基于Ajax+div的“左边菜单、右边内容”页面效果实现
- windows7 鼠标右键菜单出现在光标左边
- Mybatis基础入门之(二)——环境搭建
- Linux下文件操作命令
- 素数回文
- 随机数的产生
- SimpleAdapter动态加载Bitmap
- 完美实现淘宝易迅左边菜单树,鼠标over事件显示菜单内容
- android 常用URI 值得记住
- 1.1 C++程序结构 (Structure of a program)
- Spring AOP
- 动态函数调用实现下列操作,输入2个数以及操作符计算结果。 @ 求最大公约数 $求最小公倍数 - 求差 + 求和 等等
- poj 1129
- //动态函数调用实现下列操作,输入2个数以及操作符计算结果。@ 求最大公约数 $求最小公倍数 - 求差 + 求和 等等
- JavaScript判断浏览器类型及版本
- Headmaster's Headache - UVa 10817 状压dp