首页 > 运营 > 帮助中心 > 正文

ECSHOP实现商品颜色尺寸仿淘宝选择功能教程

2019-10-16 21:46:20
字体:
来源:转载
供稿:网友

注:此功能与具体的模板相关,无法写出通用教程.因为,此帖子以讲原理为主. 
请大家运用时举一反三,不要生搬硬套代码.
也因为同样的原因,此篇帖子不回帖因个人安装导致的问题,只解决确实属于代码漏洞的问题.

原理:
1:css控制本来要显示的radio为一小矩形.
2:当点击此小矩形时,js使此radio选中,同时改变此小矩形的外观样式.

牵涉到的修改文件(default模板为例)
/themes/default/style.css
/themes/default/goods.dwt
注:此路径待修改模板路径.

修改步骤:
一:控制样式
1.打开/themes/ecshop/images
加添图片test.gif.

2.打开/themes/ecshop/style.css
最下面添加:

  1. /*--------------颜色选择器CSS添加-------------*/
  2.  
  3. .catt{width:100%;height:auto;overflow:hidden;padding-bottom:5px;}
  4.  
  5. .catt a{border: #c8c9cd 1px solid;  text-align: center; background-color: #fff; margin-left:5px;margin-top:6px;padding-left: 10px;padding-right: 10px;display: block; white-space: nowrap; color: #000; text-decoration: none;float:left;}
  6.  
  7. .catt a:hover {border:#ff6701 2px solid; margin: -1px; margin-left:4px;margin-top:5px;}
  8.  
  9. .catt a:focus {outline-style:none;}
  10.  
  11. .catt .cattsel {border:#ff6701 2px solid; margin: -1px;background: url("images/test.gif") no-repeat bottom right; margin-left:4px;margin-top:5px;}
  12.  
  13. .catt .cattsel a:hover {border: #ff6701 2px solid;margin:-1px;background: url("images/test.gif") no-repeat bottom right;}
复制代码
3.打开/themes/ecshop/goods.dwt

注:以下修改以原版ecshop2.7.0版本default(模板名称)为基准

未修改前第347-351行
  1. <!-- {foreach from=$spec.values item=value key=key} -->
  2.  
  3. <label for="spec_value_{$value.id}">
  4.  
  5. <input type="radio" name="spec_{$spec_key}" value="{$value.id}" id="spec_value_{$value.id}" {if $key eq 0}checked{/if} onclick="changePrice()" />
  6.  
  7. {$value.label} [{if $value.price gt 0}{$lang.plus}{elseif $value.price lt 0}{$lang.minus}{/if} {$value.format_price|abs}] </label><br />
  8.  
  9. <!-- {/foreach} -->
复制代码
修改为:
  1. <div class="catt">
  2.  
  3. <!-- {foreach from=$spec.values item=value key=key} -->
  4.  
  5.   <a {if $key eq 0}class="cattsel"{/if} onclick="changeAtt(this)" href="javascript:;" name="{$value.id}" href="tag.php?name=%CC%D4%B1%A6" style="padding: 0px; margin: 0px; color: rgb(51, 51, 51); font-family: Simsun; font-size: 14px; word-wrap: break-word; cursor: pointer; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(255, 0, 0); white-space: nowrap; ">淘宝上那种小矩形样式显示在页面.

    二:增加js控制样式与选中行为
    在页面内找到<script>这样的东西,在其后面添加一个js函数
    1. function changeAtt(t) {
    2. t.lastChild.checked='checked';
    3. for (var i = 0; i<t.parentNode.childNodes.length;i++) {
    4.         if (t.parentNode.childNodes[i].className == 'cattsel') {
    5.             t.parentNode.childNodes[i].className = '';
    6.         }
    7.     }
    8. t.className = "cattsel";
    9. changePrice();
    10. }
    复制代码

     

    此处是为了控制 选择页面上的颜色 同时也选中了对应的隐藏input 从而来更改商品属性.如:价钱...等.
    如果是用户修改过的goods.dwt可搜索:function changePrice()在其上面添加.

    说明:
    1.以上修改会将商品属性部分所有radio修改.
    2.由于用户goods.dwt可能都被修改过,用户请根据自己的实际情况修改.
    3:对于颜色选择功能,这是目前本论坛上最简单的实现方法.

    文中所用附件 test.gif如下:

    ECSHOP实现商品颜色尺寸仿淘宝选择功能教程

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表