因为近期给朋友公司做个门户网站,把荒置了6、7年的PHP又重新拾起,发现PHP这些年兴旺多了,很多新的东西看的不明不白,研究了几个框架ZendFramework、thinkphp、Symfony、yii2,也曾经考虑直接用网上现有的开源cms,要么因为商用授权太贵,要么后台太复杂,要么覆盖行业太多导致业务复杂,看了一通代码,头晕得很,最终选择yii2自己开发个轻量的cms+shop系统,慢慢的做,应该能做出后台简单、有自己特色的网站系统来!一下是我的一些开发过程和经验,因为没有考虑太多的技术和通用,所以有局限。网友可参考使用(请注明出处即可,我也使用了别人的成果)

在yii2中把一些js的动态组件封装成可参数化的Widget对象能够简化功能逻辑,代码复用可大大提高。

一般组件由样式、js脚本、html标签构成,在yii2中被放到AssetBundle和Widget中处理。Asset Bundle这个英文的解释是把有用的东西捆扎在一块,对的!它就是把一些公共的css和js捆扎成自定义的包,Widget用来放置客户化的样式、js脚本、html标签输出到浏览器。

一下是对SuperSlide2中的“焦点图 / 幻灯片”(来源http://www.SuperSlide2.com/)的Widget封装。

首先从AssetBundle继承出自己的SuperSlideAsset(文件名为SuperSlideAsset.php),把jquery.superslide.2.1.1.js和它使用样式捆扎,代码如下:

<?php

namespace yii\widgets;

use yii\web\AssetBundle;
use yii\web\View;

class SuperSlideAsset extends AssetBundle
{
public $sourcePath = '@yii/assets/superslide';
public $js = [
'jquery.superslide.2.1.1.js',
];

public $css = [
'themes/default/default.css',
];
public $depends = [
'yii\web\YiiAsset',
];
}

文件jquery.superslide.2.1.1.js和它使用样式放到这个路径:

然后从Widget继承编写自己的SuperSlideWidget,主要东西有init()初始化参数、注册js和css文件、输出html动态标签。代码如下:

<?php

namespace yii\widgets;

use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\base\Arrayable;
use yii\i18n\Formatter;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\helpers\ArrayHelper;

class SuperSlideWidget extends Widget
{
const PLUGIN_NAME = 'SuperSlide';

/**
* SuperSlide Options
* @var array
*/
public $rows;
public $name;
public $width;
public $height;
public $themeType;//default

public $autoPlay;//true,false

//[v1.0] fade:渐显; || top:上滚动;|| left:左滚动;|| topLoop:上循环滚动;|| leftLoop:左循环滚动;|| topMarquee:上无缝循环滚动;|| leftMarquee:左无缝循环滚动;
//[v2.0] fold:淡入淡出;[v2.1] slideDown:下拉效果
public $effect;//动画效果
public $trigger;//"mouseover" titCell触发方式 || mouseover:鼠标移过触发;|| click:鼠标点击触发;
public $easing;//"swing" 缓动效果;[v2.1]更改默认效果为“swing”,使效果更流畅
public $delayTime;//毫秒;切换效果持续时间(一次切换效果执行所用的时间长度)。
public $mouseOverStop;//true 鼠标移到容器层(无缝滚动是mainCell)是否停止播放
public $pnLoop; //true 前/后按钮是否继续循环,若为false则当翻动到最前/后页时,前/后按钮点击无效,同时增加prevStop/nextStop类名控制样色

/**
* Initializes the widget.   init()初始化参数、
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
if ($this->name === null) {
throw new InvalidConfigException("'name' properties must be specified.");
}
if ($this->rows === null) {
throw new InvalidConfigException("'rows' properties must be specified.");
}
if ($this->width === null) {
$this->width="1024px";
}
if ($this->height === null) {
$this->height="400px";
}
if ($this->autoPlay === null) {
$this->autoPlay="true";
}
if ($this->effect === null) {
$this->effect="fade";
}
if ($this->trigger === null) {
$this->trigger="mouseover";
}
if ($this->easing === null) {
$this->easing="swing";
}
if ($this->delayTime === null) {
$this->delayTime="500";
}
if ($this->mouseOverStop === null) {
$this->mouseOverStop="true";
}
if ($this->pnLoop === null) {
$this->pnLoop="true";
}
if ($this->themeType === null) {
$this->themeType="default";
}
parent::init();
}
/**
* @inheritdoc,
titCell ".hd li" 导航元素对象(鼠标的触发元素对象) 图解
mainCell ".bd" 切换元素的包裹层对象

*/
public function run()
{
$this->registerClientScript();//注册js和css文件

//输出html动态标签
$out= "<div id='".$this->name."' class='slideBox' width='".$this->width."' height='".$this->height."' >\n";
$out=$out. "<div class='hd'>\n";
$iRow=1;
$hdul=" <ul style='overflow:hidden; zoom:1; float:left;margin:0; padding:0; list-style-type:none;'>\n";
while ($iRow<count($this->rows)+1 )
{
$hdul=$hdul."<li>".$iRow."</li>";
$iRow=$iRow+1;
}
$hdul=$hdul."</ul>\n";
$out=$out. $hdul;
$out=$out. "</div>\n";

$out=$out. "<div class='bd'>\n";
$out=$out. "<ul style='margin:0; padding:0;list-style-type:none;' >\n";
foreach ($this->rows as $k => $ad ) {
$out=$out."<li><a href='".$ad['link_url']."' target='".$ad['target']."'>";

$out=$out." <img src='".$ad['image_url']."' width='100%' height='".$this->height."' /></a></li>\n";

}
$out=$out. " </ul>\n";
$out=$out. "</div>\n";
$out=$out. "<!-- 下面是前/后按钮代码,如果不需要删除即可 -->\n";
$out=$out. "<a class='prev' href='javascript:void(0)'></a>\n";
$out=$out. "<a class='next' href='javascript:void(0)'></a>\n";

$out=$out. "</div>\n";

echo $out;
}

/**
* register client scripts(css, javascript)
*/
public function registerClientScript()
{
$view = $this->getView();
$asset = SuperSlideAsset::register($view); //这里使用SuperSlideAsset
if ($this->themeType != 'default') {
$view->registerCssFile($asset->baseUrl . "/themes/{$this->themeType}/{$this->themeType}.css", ['depends' => 'yii\widgets\SuperSlideAsset']);
}

$options = [];
$options['mainCell'] = '.bd ul';
$options['autoPlay'] = $this->autoPlay;
$options['effect'] = $this->effect;
$options['trigger'] = $this->trigger;
$options['easing'] = $this->easing;
$options['delayTime'] = $this->delayTime;
$options['mouseOverStop'] = $this->mouseOverStop;
$options['pnLoop'] = $this->pnLoop;

$js=" jQuery('.slideBox').slide(".Json::encode($options)."); \n";

$view->registerJs($js,3);//View::POS_END
}

}

使用SuperSlideWidget的例子:

$rows=Yii::$app->db->createCommand($sql)->query();//需要包含link_url,image_url,target字段

echo SuperSlideWidget::widget([
'name' => 'slide_windows',
'width' => '1024px',
'rows'=> $rows,
'height' => '350px',
'themeType' => 'default',
])
至此,大功告成!

参考样式文件:

.slideBox{ background:#fff;overflow:hidden; position:relative; border:1px solid #ddd;}
.slideBox .hd{ height:15px; overflow:hidden; position:absolute; right:5px; bottom:5px; z-index:1; }
.slideBox .hd ul{ overflow:hidden; zoom:1; float:left; }
.slideBox .hd ul li{ float:left; margin-right:2px; width:15px; height:15px; line-height:14px; text-align:center; background:#fff; cursor:pointer; }
.slideBox .hd ul li.on{ background:#f00; color:#fff; }
.slideBox .bd{ position:relative; height:100%; z-index:0; }
.slideBox .bd li{ zoom:1; vertical-align:middle; }
.slideBox .bd img{ display:block;border:0; }

.slideBox .a{ text-decoration:none; color:#333; font:normal 12px/22px 宋体; }

/* 下面是前/后按钮代码,如果不需要删除即可 */
.slideBox .prev{ position:absolute; right:0px; top:48%; margin-top:0px; display:block; width:32px; height:40px; background:url(../images/l.png) no-repeat; filter:alpha(opacity=50);opacity:0.5; }
.slideBox .next{ position:absolute; left:auto; top:48%; margin-top:0px; display:block; width:32px; height:40px; background:url(../images/r.png) no-repeat; filter:alpha(opacity=50);opacity:0.5; }
.slideBox .prev:hover,
.slideBox .next:hover{ filter:alpha(opacity=100);opacity:1; }
.slideBox .prevStop{ display:none; }
.slideBox .nextStop{ display:none; }

PHP在yii2中封装SuperSlide 幻灯片编写自己的SuperSlideWidget的例子的更多相关文章

  1. 4.3.6 对象的界定通过编写接口来访问带这类命名结构的表会出问题。如前所述,SQL Server的灵活性不应用作编写错误代码或创建问题对象的借口。 注意在使用Management Studio的脚本工具时,SQL Server会界定所有的对象。这不是因为这么做是必须的,也不是编写代码的最佳方式,而是因为在界定符中封装所有的对象,比编写脚本引擎来查找需要界定的对象更容易。

    如前所述,在创建对象时,最好避免使用内嵌的空格或保留字作为对象名,但设计人员可能并没有遵守这个最佳实践原则.例如,我当前使用的数据库中有一个审核表名为Transaction,但是Transaction ...

  2. 示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写

    原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard.Animation用于简化动画编写 一.目的:通过对StoryBoard和Animation的封装来简化动画 ...

  3. 关于在VB.NET中调用使用VC++编写的类库dll的一点笔记

    前言 结对作业要求一出来,我就立刻想到了把“计算核心”封装成dll,然后使用vb.net编写UI调用dll的思路.然而在实现过程中却遇到了很多的问题. 我在这个过程中是负责使用vb.net编写UI并调 ...

  4. YII2中验证码的使用

    验证码的使用是比较频繁的.YII2中已经帮我们做好了封装. 首先我们在控制器里创建一个actions方法,用于使用yii\captcha\CaptchaAction <?php namespac ...

  5. YII2中分页组件的使用

    当数据过多,无法一页显示时,我们经常会用到分页组件,YII2中已经帮我们封装好了分页组件. 首先我们创建操作数据表的AR模型: <?php namespace app\models; use y ...

  6. yii2中关联查询

    yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecor ...

  7. 以SqlHelper为例论面向对象中封装的使用

    引言: 在使用面向对象方法编写的程序中,会有一些工具类,如Utility,xxHelper等. 比如1)操作数据库的过程,一般步骤都是:1.准备数据库地址.表名等信息:2.建立连接:3.准备要执行sq ...

  8. yii2中如何使用modal弹窗之基本使用

    作者:白狼 出处:http://www.manks.top/yii2_modal_baseuse.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接, ...

  9. Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order      (id  order_name ...

随机推荐

  1. GZipStream 压缩和解压

    GZipSteam: GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法 类 GZipStream有两种模式:CompressionMode.Compress和CompressionMode ...

  2. beego 框架入门

    根据官网向导安装配置好环境和工具https://beego.me 就可以开始了,先来入门下. 1.新建项目  在项目目录下 bee new quickstart成功后就可以运行了 http serve ...

  3. https适配

    http://www.jianshu.com/p/f312a84a944c http://www.2cto.com/kf/201611/570823.html http://www.cnblogs.c ...

  4. JS总结 循环 退出循环 函数

    while循环 while(条件){条件成立就执行的代码} *一般条件变量需要递增,否则会进入死循环(无限循环),浏览器会崩溃甚至电脑死机 例如,逐行输出1-100的数字 var i = 1; whi ...

  5. Typescript中的class interface 只是在声明,其实什么也没有干!

    由于vue.js的特殊机制,初始化后给对象添加的属性是无法更新UI的. 最近结合typescript和vue进行web开发,就遇到了这样的坑. class user{ name:string; age ...

  6. C#语言Winform防SQl注入做用户登录的例子

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. gantt甘特图的制作过程

    甘特图主要是用来做项目管理的,可以清楚的看到任务间的逻辑关系,任务与时间关系和任务间并行关系. 在甘特图中,横轴方向表示时间,纵轴方向并列着活动列表.图表内可以用线条.数字.文字代号等来表示计划(实际 ...

  8. 服务器列表里找不到OracleOraDb10g_home1TNSListener

    先安装的ORACLE DB,后配置的数据库,TNSNAMES.ORA, LISTENER.ORA配置完毕,客户端连接不上TNS服务器,发现服务列表里没有OracleOraDb10g_home1TNSL ...

  9. u-boot学习笔记(一):基础概念

    1.U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导.U-Boot从FADSROM.8xxROM.PPCBOOT逐步发展演化 ...

  10. ACCESS --第一章

    一.ACCESS2007的安装 装完整版的office2007办公软件就集成了相应的ACCESS2007 二.主要组成 ACCESS由表.查询.报表.窗体.页.宏和模块组成 1.表 表是存储数据的对象 ...