有不少人想做京東商城分類效果,搜尋了一下之前的文章,發現大蝦們一般都是直接下
載了京東的代碼并在其基礎上改。但這樣會出現一個問題,就是ecshop不相容jquery,
而這個問題解決起來比較複雜,更不好的是它會破壞ecshop的内部函數,不利于以後版
本更新。
大家不需要盲目迷信大站,其實京東商城的分類效果完全可以用純DIV+CSS實作,不需
要jquery,甚至一句javascript都不寫也能實作。
廢話少說,先上圖

然後上代碼
第一步:在你所用的模闆的css檔案中加上以下代碼
- .my_left_category{
- width:211px;
- font-size:12px;
- }
- .my_left_category h1{
- background-p_w_picpath:url(p_w_picpaths/spring_06.jpg);
- height:20px;
- background-repeat:no-repeat;
- font-size:14px;
- font-weight:bold;
- padding-left:15px;
- padding-top:8px;
- margin:0px;
- color:#FFF;
- .my_left_category .my_left_cat_list{
- width:209px;
- border-color:#ce2020;
- border-style:solid;
- border-width:0px 1px 1px 1px;
- line-height:13.5pt;
- .my_left_category .my_left_cat_list h2 {
- padding:3px 5px 0px 9px;
- .my_left_category .my_left_cat_list h2 a{
- color:#d6290b;
- line-height:22px;
- .my_left_category .h2_cat{
- height:26px;
- background-p_w_picpath:url(p_w_picpaths/my_menubg.gif);
- line-height:26px;
- font-weight:normal;
- color:#333333;
- position:relative;
- .my_left_category a{
- font:12px;
- text-decoration:none;
- .my_left_category a:hover{
- text-decoration:underline;
- color:#ff3333;
- .my_left_category h3{
- padding:0px;
- display:block;
- padding-left:8px;
- .my_left_category h3 span{color:#999999; width:145px; float:right;}
- .my_left_category h3 a{ line-height:26px;}
- .my_left_category .h3_cat{
- display:none;
- width:204px;
- position:absolute;
- left:184px;
- margin-top:-26px;
- cursor:auto;
- .my_left_category .shadow{
- position:inherit;
- background:url(p_w_picpaths/shadow_04.gif) left top;
- .my_left_category .shadow_border{
- width:200px;
- border:1px solid #959595; margin-top:1px;
- border-left-width:0px;
- background:url(p_w_picpaths/shadow_border.gif) no-repeat 0px 21px;
- background-color:#ffffff;
- margin-bottom:3px
- .my_left_category .shadow_border ul{margin:0; padding:0; margin-left:15px}
- .my_left_category .shadow_border ul li {
- list-style:none;
- padding-left:10px;
- background-p_w_picpath:url(p_w_picpaths/my_cat_sub_menu_dot.gif);
- background-position:0px 8px;
- float:left;
- width:75px;
- overflow:hidden;
- .my_left_category .active_cat{ z-index:99;background-position:0 -25px;cursor:pointer;}
- .my_left_category .active_cat h3 { font-weight:bold}
- .my_left_category .active_cat h3 span{ display:none;}
- .my_left_category .active_cat div{display:block;}
複制代碼
第二步:模闆檔案夾的library/category_tree.lbi内容改為:
- <meta http-equiv="Content-Type" content="text/html; charset=gbk">
- <div class="my_left_category">
- <h1>商品分類</h1>
- <div class="my_left_cat_list">
- {assign var="pre_item_level" value=-1}
- <!--{foreach from=cat_list(0,0,false,3,false) item=cat}-->
- {if $cat.level lt 2 && $pre_item_level gt 0}
- </ul></div></div></div></div>
- {/if}
- {if $cat.level eq 0}
- <h2><a href="{$cat.url}">{$cat.name|escape:html}</a></h2>
- {elseif $cat.level eq 1}
- <div class="h2_cat" onmouseover="this.className='h2_cat active_cat'" onmouseout="this.className='h2_cat'">
- <h3><SPAN> - {$cat.cat_desc|escape:html}</SPAN><a href="{$cat.url}">{$cat.name|escape:html}</a></h3>
- <div class="h3_cat">
- <div class="shadow">
- <div class="shadow_border">
- <ul>
- {elseif $cat.level eq 2}
- <li><a href="{$cat.url}">{$cat.name|escape:html}</a></li>
- {assign var="pre_item_level" value=$cat.level}
- <!--{/foreach}-->
- {if $pre_item_level gt 0}
- </div>
第三步:把用到的圖檔拷貝到模闆檔案夾的p_w_picpaths目錄
ok完成!
注:
京東的二級分類名稱旁邊列了2個三級分類名稱(灰色),作為三級分類的提示。參考其他網友的做法,這裡使用二級類的描述字段cat_desc來實作,【用程式去調取二級類下的前兩個三級分類完全是化簡為煩的事情。】但這個效果要修改 /includes/lib_common.php 才生效的,當然,不修改也不會出錯,隻是沒有顯示罷了。
修改方法:在/includes/lib_common.php 的cat_list函數中找到
$sql = "SELECT c.cat_id, c.cat_name,
修改為
$sql = "SELECT c.cat_id, c.cat_name, c.cat_desc,
補充一下:
在firefox等浏覽器中,如果展開的部分被遮擋(由于父級容器overflow:hidden引起),在.my_left_category的樣式中加上 position:absolute即可。
今天上午放出的category_tree.lbi代碼有點bug,當最後一個頂級分類下面沒有子類的時候,會生成一段多餘的“</ul></div></div></div></div>”,把26行的</ul></div></div></div></div>改為
- </ul></div></div></div></div>
- {/if}
即可
1樓的附件和代碼已更改過來,本樓之前下載下傳的朋友請更新一下