PHP在yii2中封装SuperSlide 幻灯片编写自己的SuperSlideWidget的例子

因为近期给朋友公司做个门户网站,把荒置了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‘,
])
至此,大功告成!

时间: 2024-08-05 19:35:53

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

yii2中关联查询

yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecord是怎么个多表关联以及如何去优化这个关联. 场景需求: 假设我们有一张用户表user和一张用户渠道表auth,两张数据表通过user.id和auth.uid进行一对一关联.现需要在user列表展示auth表的来源渠道source,且该渠道可搜索. 首先我们先通过gii生成user和auth系列相关的

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

前言 结对作业要求一出来,我就立刻想到了把“计算核心”封装成dll,然后使用vb.net编写UI调用dll的思路.然而在实现过程中却遇到了很多的问题. 我在这个过程中是负责使用vb.net编写UI并调用编写好的DLL进行计算的. 目标 使用c++把类封装到dll,并在vb.net中调用该dll,使用该dll中封装好的类.在查找资料的过程中,发现vb.net调用dll的方法主要有两种. (IDE:Visual Studio 2013 professional) 方法一:使用Declare语句 vb

Yii2中的模块、应用程序(Module,Application)

原文地址:http://www.kuitao8.com/20140626/2715.shtml 模块(Module ) 模块是一个功能独立的逻辑单元,每一个模块都可以包含有多个子模块,但每个模块只能有一个对应的父模块(如果有的话).它的定义在yii\base\Module 应用程序(Application) Yii2中的应用程序有两种:web应用程序(yii\web\Application)和控制台应用程序(yii\console\Application).他们都继承于yii\base\Appl

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

表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order         (id  order_name   customer_id   book_id) 图书表Book          (id  book_name    author_id) 作者表Author        (id  author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer ex

yii2中的资源....

1.模板文件中访问view和controller,view : $this,controller :$this->context 模板文件显示流程: 1.控制器会在render中,把控制器本身,模板文件传给view 2.呈现模板文件,首先view会查找主题,主题如果拥有模板文件,则优先使用主题内模板文件.并将模板文件赋值给变量$content 3.呈现布局文件,同样查找主题,并将$content作为变量导入 4.引入模板文件时,是发生在view中,所以$this就是view 2.注册资源 reg

c语言中如何设计和编写一个应用系统?

C程序中,如何设计和编写一个应用系统? 一. C语言文件的操作 1. 文件操作的基本方法: C语言将计算机的输入输出设备都看作是文件.例如,键盘文件.屏幕文件等. 向屏幕输出一个信息,例如"Hello"是 #include.h> int main() { printf("Hello\\n"); } 从键盘接收一个字符串然后显示是 #include.h> int main() { char a[10]; scanf("%s",&

yii2中添加验证码的实现方法

本文实例讲述了yii2中添加验证码的实现方法.分享给大家供大家参考,具体如下: 首先,在模型中添加验证码字段: ? 1 2 3 public function rules(){ return ['verifyCode', 'captcha'], } 其次,可以在函数attributeLabels中添加前台页面中验证码的字段名称: ? 1 2 3 public function atrributeLabels(){ return ['verifyCode'=>'Verification Code'

Yii2中多表关联查询(join、joinwith) with是不执行sql的

Yii2中多表关联查询(join.joinwith) 我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id book_id) 图书表 (id book_name author_id) 作者表 (id author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer extends \

在DLL动态链接库中封装VCL的MDI子窗体

在DLL动态链接库中封装VCL的MDI子窗体不多说了,看代码就应该明白了,曾经我遇到的问题,现在放出来大家共享! 这里是工程文件的部分: 在DLL中封装MDI子窗体需要重写DLL入口函数,具体代码如下: var DllApp: TApplication;//定义保存原DLL的TApplication对象 DllScr: TScreen;//定义保存原DLL的TScreen对象 procedure UnProcDll(Reason: Integer); register;//重新定义DLL入口函数