2008/10/24

JQuery背景褪色解决方案,需插件Color Animations

背景褪色一般用于信息更改时提醒,一般使用的黄渐变(yellow fade )。

jquery1.2以后可以使用,但需要插件Color Animations ,首先下载插件,在页面中包含进来。

使用animate动画函数,设置backgroundColor属性即可,值形式可以为 rgb(num,num,num),rgb(num%,num%,num%),#a0b1c2
,#fff,。同时适用的属性还有:['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor']。

代码1:
 
<?php
$('.block').animate( { backgroundColor: 'pink' }, 1000)
    .animate( { backgroundColor: 'blue' }, 1000);
});
?>
 
代码2:
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
  $(document).ready(function(){
    $("#go").click(function(){
  $(".block").animate( { backgroundColor: 'pink' }, 1000)
    .animate( { backgroundColor: 'blue' }, 1000);
});
  });
  </script>
<style>
.block {
   background-color: blue;
   width: 150px;
   height: 70px;
   margin: 10px;
}
</style>
</head>
<body>
  <script src="http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js"></script>
<button id="go">» Run</button>
<div class="block"></div>
</body>
</html>
 
简单说一下原理:
 
//将所有color的属性都设置一遍,以后在设置这些属性值时会调用下面定义的step函数
    jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
        jQuery.fx.step[attr] = function(fx){
            if ( fx.state == 0 ) {
                //取得当前的属性值,转为[255,255,255]的形式
                fx.start = getColor( fx.elem, attr );
                //取得设置的值,转为[255,255,255]的形式
                fx.end = getRGB( fx.end );
            }

            fx.elem.style[attr] = "rgb(" + [
                //用end-start的值乘以比率,在加上start的值,即得到每step的值。fx.pos为动画的完成比率
                Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
                Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
                Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
            ].join(",") + ")";
        }
    });
 

2008/10/23

如何在cakephp的布局文件中包含多个模板?

cake中的布局(layout)确实不错,可惜不能包含多个模板文件(view)进来。
前几天在网上看到一个实现方法,http://www.xuzhousoft.cn/content/view/48/1/ ,看了一下代码,是用helper来实现的,感觉有点费劲。从代码看,其view文件中应该不支持$session,$html之类的了。
下面介绍一款简单实用的方法。先来看看cake的layout是如何实现下面这个的:
1 <?php echo $content_for_layout; ?>

在cake/libs/view/view.php中有个方法叫做: function renderLayout()其中有$data_for_layout如下:
01 <?php
02 $data_for_layout = array_merge($this->viewVars,
03 array(
04 'title_for_layout' => $pageTitle,
05 'content_for_layout' => $content_for_layout,
06 'scripts_for_layout' => join("\n\t", $this->__scripts),
07 'cakeDebug' => $debug
08 )
09 );
10 ?>

也就是说实现的数据都是准备好的,从这一点上来讲就很难扩展了,因为变量就那么几个啊。
下面介绍一个方法,看代码:
01 <?php
02 /**
03 * 引入其他view文件,在layout中支持多个view
04 *
05 * @param string $name 在view目录下的文件
06 * @return string
07 *
08 */
09 function template($name) {
10 $s = addslashes( ROOT.DS.APP_DIR.DS.'views'.DS.$name.'.ctp' );
11 return "require(\"$s\");";
12 }
13 ?>

将这个函数添加到你的项目中去,然后在你的layout中添加如下代码即可包含多个view:
view/layout/default.ctp
1 ...
2 <?php
3 //包含模板 include you template
4 eval(template('elements'.DS.'hello'));
5 ?>
6 ...

view/elements/hello.ctp
1 <?php
2 echo 'yes!';
3 //可以支持controller中声明的所有变量和helper
4 echo $session->read('Login.name');
5 ?>

呵呵,很简单吧!

2008/10/14

摘几句《死亡诗社》的台词,百看不厌

我步入丛林

因为我希望生活有意义

我希望活的深刻

吸取生命中所有的精华

把非生命的一切都击溃

以免当我生命终结

发现自己从没有活过
 
  “I went to the woods because I wanted to live deliberately. I wanted to live deep and suck out all the marrow of life!To put to rout all that was not life. And not, when I came to die, discover that I had not lived。”

  “只有在梦想中,人才能真正自由,从来如此,并将永远如此。”
  
  “两条路在森林里分叉,我选择走的人少的那条。”

  “假如我们不能改变这个世界,那我们至少应该改变我们的生活,自由自在地活着”。遥远的日子里,米兰昆德拉如是说。

2008/10/07

新的cakephp1.2中你可以在URL中使用名字作为参数

参考:http://book.cakephp.org/cn/view/541/Named-parameters

New in CakePHP 1.2 is the ability to use named parameters. You can name parameters and send their values using the URL. A request for /posts/view/title:first+post/category:general would result in a call to the view() action of the PostsController. In that action, you’d find the values of the title and category parameters inside $this->passedArgs[‘title’] and $this->passedArgs[‘category’] respectively.

新的cakephp1.2中你可以在URL中使用名字作为参数,请求 ‘/posts/view/title:first+post/category:general’ 会自动调用PostsController中view()方法,在此action中包含了title和category的值,通过 $this->passedArgs[‘title’]和$this->passedArgs[‘category’] 来访问。

URL to controller action mapping using default routes:

默认路由下URL到控制器和Action的映射:

URL: /monkeys/jump
Mapping: MonkeysController->jump();

URL: /products
Mapping: ProductsController->index();

URL: /tasks/view/45
Mapping: TasksController->view(45);

URL: /donations/view/recent/2001
Mapping: DonationsController->view('recent', '2001');

URL: /contents/view/chapter:models/section:associations
Mapping: ContentsController->view();
$this->passedArgs['chapter'] = 'models';
$this->passedArgs['section'] = 'associations';

2008/09/27

让你的Firefox支持显示XHTML-MP

最近在开发m.dangdang.com,使用页面标记语言是xhtml-mp,IE是无法显示的。在开发时候需要使用opera来调试(opera还支持wml,wap网站还可以直接看),后来发现Firefox也可以显示xhtml-mp,安装插件即可,https://addons.mozilla.org/zh-CN/firefox/addon/1345

Firefox does not natively support the mime-type
application/vnd.wap.xhtml+xml. This is one of the possible mime-types for XHTML
Mobile Profile. This extension adds support for this mime-type. (The other,
application/xhtml+xml, is supported natively by Firefox.)


Firefox并不原生支持 mime-type为application/vnd.wap.xhtml+xml的页面,本插件使之成为可能。(Firefox是支持XHTML的)。

IE和FF打开网页不显示背景颜色,只显示图片

今天系统很奇怪,IE浏览器不显示网页背景色,图片和动画都正常,打开Firefox试了一下也不行,只有opera可以。上网搜索一下,解决方法如下:

在“控制面板”的“辅助功能选项”下有一“显示”标签,单击该标签后看到“使用高对比度”选项被勾选。清除该选项后,重新启动IE,故障消除。

其实不需要重启浏览器,直接刷新就没有问题了。 原来是自己一不小心修改了控制面板里头的选项。

2008/09/11

cakephp开发笔记(1)

1.如何开发‘xhtml-mp’的页面?
只需在布局模板头部修改成下面这样即可。注意下面第二行的,必须打印,否则php会混淆‘<?’的标记,编译出错。



<?php header('Content-type: application/vnd.wap.xhtml+xml');?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>';?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>...



2.一个构思很酷的函数a()

00210 /**
00211 * Returns an array of all the given parameters.
00212 *
00213 * Example:
00214 *
00215 * a('a', 'b')
00216 *

00217 *
00218 * Would return:
00219 *
00220 * array('a', 'b')
00221 *

00222 *
00223 * @return array Array of given parameters
00224 */
00225 function a() {
00226 $args = func_get_args();
00227 return $args;
00228 }


3.我安装cakephp1.2出现的问题


3.1. .php文件无法执行

解决办法:将php的目录添加到系统环境变量中去。

3.2. 找不到php_exif.dll的扩展

原因是必须在php_mbstring.dll之后加载再加载php_exif.dll。php_exif.dll(EXIF 函数库)需要php_mbstring.dll,并且在 php.ini中,php_exif.dll必须在php_mbstring.dll之后加载。


4.cake bake的几个常用方法(cakephp1.2,已将.../cake/console添加windows环境变量)


创建新的应用,名为baz
cake bake baz

创建model(以下均需cmd到目录:D:\wamp\www\cake\baz(windows))
cake bake model [model name]

创建controller
cake bake controller Users

使用脚手架。额外创建:index, add, view, edit, and delete方法
cake bake controller Users scaffold

带有admin_index, admin_add, admin_view, admin_edit and admin_delete的脚手架
cake bake controller Users scaffold admin

创建视图
cake bake view Users


5.cakephp中取消方法vendor(),改为App::import('Vendor' , '...');

Description

The code I had before was

vendor('Swift');
vendor('EasySwift');
vendor('Swift/Connection/SMTP');
But the latest nightly told me "(vendor) Deprecated, see App::import('Vendor', '...');". So I modified my code to say

App::import('Vendor', 'Swift');
App::import('Vendor', 'EasySwift');
App::import('Vendor', 'Swift/Connection/SMTP');
Unfortunately, the classes no longer loaded. I did some poking around and it seems that App::import('Vendor', ...) doesn't know not to inflect the file names its given.

cakephp生成URL利器:Router::url()

cakephp版本为1.2。

在cakephp中,$html->link() , $html->image() 等方法的第二个参数均为url,都调用了Router::url($url = null, $full = false)。

常见用法:
1.生成url。
假设网站url路径是 http://www.example.com/project-text/。那么下面三者产生的url效果是相同的。
Router::url( array('controller'=>'category' , 'action'=>'list' , $params1, $params2 ) );
Router::url( '/category/list/params1/params2' );
Router::url( 'http://www.example.com/project-text/category/list/params1/params2' );

url均为 http://www.example.com/project-text/category/list/params1/params2' ,前两者产生的均为相对路径,第三个为全路径,需要生成完全路径只需将url()中的第二个参数设为true即可。

2.其他
Router::url( 'javascript:alert("s")' );

Router::url( 'mailto:bit.kevin@gmail.com' );