Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268447
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-03-04 02:13:08


是一款非常流行的基于jQuery的Lightbox插件,非常适用于Magento的产品页面的图片展示,下面将介绍怎样把ColorBox集成到Magento中。

1 加载jQuery
首先在Magento的页面中加载jQuery库,并下载ColorBox并放在网站的根目录的js目录下,比如/js/colorbox

2 建立模块
在/app/code/local/MagentoBoy/Colorbox目录下新建一个模块MagentoBoy_Colorbox,并添加模块文件:
/app/etc/modules/MagentoBoy_Colorbox.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_Colorbox>
  5.             <active>true</active>
  6.             <codePool>local</codePool>
  7.         </MagentoBoy_Colorbox>
  8.     </modules>
  9. </config>
并添加配置文件
/app/code/local/MagentoBoy/Colorbox/etc/config.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MagentoBoy_Colorbox>
  5.             <version>0.1.0</version>
  6.         </MagentoBoy_Colorbox>
  7.     </modules>
  8. </config>

3 添加Layout文件
/app/design/frontend/default/default/layout/colorbox.xml
  1. <?xml version="1.0"?>
  2. <layout>
  3.     <catalog_product_view>
  4.         <reference name="head">
  5.             <action method="addItem"><type>js</type><name>colorbox/jquery.colorbox-min.js</name></action>
  6.             <action method="addItem"><type>js_css</type><name>colorbox/colorbox.css</name></action>
  7.         </reference>
  8.         <reference name="product.info.media">
  9.             <action method="setTemplate"><template>colorbox/media.phtml</template></action>
  10.         </reference>
  11.     </catalog_product_view>
  12.     <review_product_list>
  13.         <reference name="head">
  14.             <action method="addItem"><type>js</type><name>colorbox/jquery.colorbox-min.js</name></action>
  15.             <action method="addItem"><type>js_css</type><name>colorbox/colorbox.css</name></action>
  16.         </reference>
  17.         <reference name="product.info.media">
  18.             <action method="setTemplate"><template>colorbox/media.phtml</template></action>
  19.             <action method="disableGallery"/>
  20.         </reference>
  21.     </review_product_list>
  22. </layout>
并在config.xml中添加layout文件
  1. <config>
  2.     <frontend>
  3.         <layout>
  4.             <updates>
  5.                 <colorbox>
  6.                     <file>colorbox.xml</file>
  7.                 </colorbox>
  8.             </updates>
  9.         </layout>
  10.     </frontend>
  11. </config>

4 修改template文件
/app/design/frontend/default/default/template/colorbox/media.phtml
  1. <?php
  2.     $_product = $this->getProduct();
  3.     $_helper = $this->helper('catalog/output');
  4. ?>
  5. <?php if ($_product->getImage() != 'no_selection' && $_product->getImage()): ?>

  6. <p class="product-image product-image-zoom">
  7.     <a href="helper('catalog/image')->init($_product, 'image');?>" class="mustang-gallery" title="htmlEscape($this->getImageLabel());?>">
  8.         <img id="image" src="helper('catalog/image')->init($_product, 'image');?>" alt="htmlEscape($this->getImageLabel());?>" title="htmlEscape($this->getImageLabel());?>" style="width:265px;" />
  9.     </a>
  10. </p>

  11. <p class="zoom-notice" id="track_hint"><?php echo $this->__('Click on above image to view full picture') ?></p>

  12. <?php else: ?>
  13. <p class="product-image">
  14.     <?php
  15.         $_img = '.$this->helper('catalog/image')->init($_product, 'image')->resize(265).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
  16.         echo $_helper->productAttribute($_product, $_img, 'image');
  17.     ?>
  18. </p>
  19. <?php endif; ?>

  20. <?php if (count($this->getGalleryImages()) > 0): ?>
  21. <div class="more-views">
  22.     <h2><?php echo $this->__('More Views') ?></h2>
  23.     <ul>
  24.     <?php foreach ($this->getGalleryImages() as $_image): ?>
  25.         <li>
  26.             <a href="helper('catalog/image')->init($_product, 'image', $_image->getFile());?>" class="mustang-gallery" title="htmlEscape($this->getImageLabel());?>"><img src="init($_product, 'thumbnail', $_image->getFile())->resize(56);?>" width="56" height="56" alt="htmlEscape($_image->getLabel()) ?>" /></a>
  27.         </li>
  28.     <?php endforeach; ?>
  29.     </ul>
  30. </div>
  31. <?php endif; ?>

  32. <script type="text/javascript">
  33. //<![CDATA[
  34. var $j = jQuery.noConflict();
  35. $j(document).ready(function(){
  36.     $j(".mustang-gallery").colorbox({
  37.         opacity :0.5,
  38.         rel :'mustang-gallery',
  39.         ransition :"fade",
  40.         height :"75%"
  41.     });
  42. });
  43. //]]>
  44. </script>
清除缓存,刷新前台页面,点击产品的图片,将弹出图片的放大图片,如果需要其他效果,可以根据ColorBox的其他参数进行设置。

阅读(1282) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~