首 页  >>  E商学院  >>  zencart百科  >>  Zen Cart 简易实现LightBox灯箱效果

Zen Cart 简易实现LightBox灯箱效果

于文前 先简要介绍一下
什么是LightBox灯箱效果,也有称之为遮罩层效果,
这是一种近年来随AJAX技术广泛应用后,
逐渐被人们所熟悉和喜爱的一种WEB前端展示效果,
这种效果将原来需要弹出新窗口的内容页面直接在当前页面无刷新加载显示,
显示对应的内容同时一般伴随着将背景变暗或反色,
以高亮突出当前内容区块,
点击非内容区域时无刷新返回原内容展示页,
整个展示效果如同城市夜晚随处可见的广告灯箱效果,故而得名 LightBox
(灯箱效果);
如下图示 (点击图片查看大图 同时本文这种图片展示效果也是一种标准的灯箱展示效果):


目前基于各个JS库的LightBox插件比比皆是, 在进行前端设计时, 可随意按需按喜好取用,


回到本文正题: 在Zen Cart模板制作时, 大多也会采用这种效果用来展示商品页的诸图片;
基于 Zen Cart的 LightBox
模块也有数多个, 比如本博曾经发布过的 JQlightbox+JQZoom
for Zen Cart 修改版,


这些模块与本文所介绍的实现方式相较来说 其结果都是一样的, 区别只在于 本文所使用的方法,
不涉及数据库 不涉及后台 只单纯的为商品页面增加 LightBox 灯箱效果 涉及的改动较少,
不影响程序本身的健壮性和日后更新维护拓展; 不需其他模块支持, 兼容性较好!
比较适合刚接触Zen
Cart的新人掌握与使用;
本文介绍和使用的 LightBox基于 JQuery 库 可在下面的地址中 下载和查看详细的介绍与配置整合说明
http://leandrovieira.com/projects/jquery/lightbox/


下面开始操作:
操作前 需要先在当前网站引入 JQuery 库 版本不限 建议 1.42以上
本文教程
直接使用 Google CDN代码托管引入
实际操作中 可按需选择托管还是本地加载,


将上面下载到本地的 JQlightBox 打包文件解压后,
将其下的 images 文件夹中的诸图标文件复制到当前网站根目录下的 images
目录下;
将 js 文件夹下的 jquery.lightbox-0.5.pack.js 文件复制到自己当前模板目录下的 jscript
文件夹下,
将 css文件夹下的 jquery.lightbox-0.5.css 文件复制到自己当前模板目录下的 CSS 文件夹下.


打开 includes/templates/你的模板/common/html_header.php
如果没有上面的文件或文件夹 
就去 默认的 模板 template_default 目录下 复制一份过来,
(此步骤强烈建议如此操作 但不是必需操作项)
至于为何这样作 可参见 
Zen
Cart的文件替代机制


然后在 </head> 前 引入 JQ库 和 JQlightBox JS库 及 JQlightBox样式表文件
代码如下:








1
2
3
4
5
6
7
8
9
10


<link rel="stylesheet" type="text/css" href="includes/templates/你的模板目录名/css/jquery.lightbox-0.5.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="includes/templates/你的模板目录名/jscript/jquery.lightbox-0.5.pack.js"></script>
<script type="text/javascript">
 <!--
  $(function() {
    $('#productMainImage a,#productAdditionalImages a').lightBox();
  });
 //-->
</script>


这里特别说明一下, 如果采取的是本地加载的方式,
则可以直接将 JQuery 库和 JQlightbox JS库命名 为 jscript_
开头的任意文件名,
然后将其粘贴到当前模板目录下的 jscript 文件夹下 则Zen Cart程序默认会自动加载 ,
而不需要上面的文件修改.
但命名时需要注意, JQuery 库的文件名一定要优先于 其他JQ插件命名的优先级,
即一定要先于其他JQ插件被加载,
否则将导致先于JQuery 库加载的插件无法正常运行 切记 很重要!!!


然后 打开 includes/modules/你的模板目录名/additional_images.php
没有对应的文件就去
includes/modules/下 复制一份同名文件过来,
清空原文件里面的代码后将下面的代码复制进去 并保存退出








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113


<?php /**
 * additional_images module
 *
 * Prepares list of additional product images to be displayed in template
 *
 * @package templateSystem
 * @copyright Copyright 2003-2007 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: additional_images.php 6132 2007-04-08 06:58:40Z drbyte $
 *
 * @ 商品多图 灯箱效果
 * @ By KIRA
 * @ 6171718
 * @ http://zcbk.org/
 *
 */ if (!defined('IS_ADMIN_FLAG')) {die('Illegal Access');} if(!defined('LARGE_IMAGE_MAX_WIDTH')) define('LARGE_IMAGE_MAX_WIDTH', MEDIUM_IMAGE_WIDTH); if(!defined('LARGE_IMAGE_MAX_HEIGHT')) define('LARGE_IMAGE_MAX_HEIGHT', MEDIUM_IMAGE_HEIGHT); if (!defined('IMAGE_ADDITIONAL_DISPLAY_LINK_EVEN_WHEN_NO_LARGE')) define('IMAGE_ADDITIONAL_DISPLAY_LINK_EVEN_WHEN_NO_LARGE','Yes'); $images_array = array(); if ($products_image != '') { $products_image_extension = substr($products_image, strrpos($products_image, '.')); $products_image_base = str_replace($products_image_extension, '', $products_image); if (strrpos($products_image, '/')) { $products_image_match = substr($products_image, strrpos($products_image, '/')+1); $products_image_match = str_replace($products_image_extension, '', $products_image_match) . '_'; $products_image_base = $products_image_match; } $products_image_directory = str_replace($products_image, '', substr($products_image, strrpos($products_image, '/'))); if ($products_image_directory != '') { $products_image_directory = DIR_WS_IMAGES . str_replace($products_image_directory, '', $products_image) . "/"; } else { $products_image_directory = DIR_WS_IMAGES; } $file_extension = $products_image_extension; $products_image_match_array = array(); if ($dir = @dir($products_image_directory)) { while ($file = $dir->read()) { if (!is_dir($products_image_directory . $file)) { if (substr($file, strrpos($file, '.')) == $file_extension) { if(preg_match("/" . $products_image_base . "/i", $file) == 1) { if ($file != $products_image) { if ($products_image_base . str_replace($products_image_base, '', $file) == $file) { $images_array[] = $file; } else { } } } } } } if (sizeof($images_array)) { sort($images_array); } $dir->close(); } } $num_images = sizeof($images_array); $list_box_contents = ''; $title = ''; if ($num_images) { $row = 0; $col = 0; if ($num_images < IMAGES_AUTO_ADDED || IMAGES_AUTO_ADDED == 0 ) { $col_width = floor(100/$num_images); } else { $col_width = floor(100/IMAGES_AUTO_ADDED); } for ($i=0, $n=$num_images; $i<$n; $i++) { $file = $images_array[$i]; $products_image_large = str_replace(DIR_WS_IMAGES, DIR_WS_IMAGES . 'large/', $products_image_directory) . str_replace($products_image_extension, '', $file) . IMAGE_SUFFIX_LARGE . $products_image_extension; if(defined(IH_RESIZE)) $flag_has_large = true; else $products_image_large = ($flag_has_large ? $products_image_large : $products_image_directory . $file); $flag_display_large = (IMAGE_ADDITIONAL_DISPLAY_LINK_EVEN_WHEN_NO_LARGE == 'Yes' || $flag_has_large); $base_image = $products_image_directory . $file; $thumb_slashes = zen_image($base_image, addslashes($products_name), SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT); $thumb_slashes = preg_replace("/([^\\\\])'/", '$1\\\'', $thumb_slashes); $thumb_regular = zen_image($base_image, $products_name, SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT); $large_link = zen_href_link(FILENAME_POPUP_IMAGE_ADDITIONAL, 'pID=' . $_GET['products_id'] . '&pic=' . $i . '&products_image_large_additional=' . $products_image_large); $noscript_link = '<noscript>' . ($flag_display_large ? '<a href="' . zen_href_link(FILENAME_POPUP_IMAGE_ADDITIONAL, 'pID=' . $_GET['products_id'] . '&pic=' . $i . '&products_image_large_additional=' . $products_image_large) . '" target="_blank">' . $thumb_regular . '<br /><span class="imgLinkAdditional">' . TEXT_CLICK_TO_ENLARGE . '</span></a>' : $thumb_regular ) . '</noscript>'; $lightBox_img = '' . ($flag_display_large ? '<a href="' . $products_image_large.'">' . $thumb_regular . '</a>' : $thumb_regular ) . ''; $link = $lightBox_img . "\n " . $noscript_link; $list_box_contents[$row][$col] = array('params' => 'class="additionalImages centeredContent back"' . ' ' . 'style="width:' . $col_width . '%;"', 'text' => "\n " . $link); $col ++; if ($col > (IMAGES_AUTO_ADDED -1)) { $col = 0; $row ++; } } }


然后前台看一下 商品的多图 就以经具备灯箱展示效果
接下来 继续给商品主图也加上 灯箱效果
打开
includes/templates/你的模板目录名/templates/tpl_modules_main_product_image.php








1
2
3


<script language="javascript" type="text/javascript"><!-- document.write('<?php echo '<a href="javascript:popupWindow(\\\'' . zen_href_link(FILENAME_POPUP_IMAGE, 'pID=' . $_GET['products_id']) . '\\\')">' . zen_image($products_image_medium, addslashes($products_name), MEDIUM_IMAGE_WIDTH, MEDIUM_IMAGE_HEIGHT) . '<br /><span class="imgLink">' . TEXT_CLICK_TO_ENLARGE . '</span></a>'; ?>'); //--></script>


替换为 下面代码即可








1


<?php echo '<a href="images/'.$products_image.'" title="'.$products_name.'">' . zen_image($products_image_medium, $products_name, MEDIUM_IMAGE_WIDTH, MEDIUM_IMAGE_HEIGHT) . '</a>';?>


至此 整个商品页的图片展示全部被赋予了灯箱效果,


如果你对代码还具有点先天性的洁癖和强迫症的话 也可以考虑为上面的诸支持库文件的引入加一个页面条件判读
比如说 只在商品页面加载 就改成
下面的代码








1
2
3
4
5
6
7
8
9
10
11
12


<?php if ($current_page_base == 'product_info') { ?> <link rel="stylesheet" type="text/css" href="includes/templates/你的模板目录名/css/jquery.lightbox-0.5.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="includes/templates/你的模板目录名/jscript/jquery.lightbox-0.5.pack.js"></script> <script type="text/javascript"> <!-- $(function() { $('#productMainImage a,#productAdditionalImages a').lightBox(); }); //--> </script> <?php } ?>


另外 也可以参照 JQlightBox 官方站的说明文档 进行更高级的效果配置
比如








1
2
3
4
5
6
7
8
9
10
11
12


<script type="text/javascript"> <!-- $(function() { $('#productMainImage a,#productAdditionalImages a').lightBox({ overlayBgColor:"#5A5A5A", overlayOpacity:0.8, containerBorderSize:10, containerResizeSpeed:400 }); }); //--> </script>


如果还想更进一步 哪种大小图随鼠标事件切换的 可以参见 一文 Zen Cart商品页多图效果 一文
继续折腾


点击次数:1701  发布日期:2013-04-15  【打印此页】  【关闭