用TimelineMAX來寫動畫是一件很簡單的事情
不過在寫連續的動畫圖,卻遇到了麻煩
```javascript
var tl = new TimelineMax({repeat:-1});
tl.to($('.Scroll'),0.1,{css:{'backgroundPosition':'-683px -222px'}});
tl.to($('.Scroll'),0.1,{css:{'backgroundPosition':'-693px -111px'}});
tl.to($('.Scroll'),0.1,{css:{'backgroundPosition':'-693px -0px'}});
tl.to($('.Scroll'),0.1,{css:{'backgroundPosition':'-595px -525px'}});
tl.to($('.Scroll'),0.1,{css:{'backgroundPosition':'-517px -525px'}});
```
如果這樣寫的時候,會發現background-position在變換的時候會由A點移動到B點,
而不是原本所預想的直接換background-position
試了很久終於想到可以用delay方式來處理就行了
所以正確的程式碼如下
```javascript
var tl = new TimelineMax({repeat:-1});
tl.to($('.Scroll'),0,{css:{'backgroundPosition':'-683px -222px'}},'+=.1');
tl.to($('.Scroll'),0,{css:{'backgroundPosition':'-693px -111px'}},'+=.1');
tl.to($('.Scroll'),0,{css:{'backgroundPosition':'-693px -0px'}},'+=.1');
tl.to($('.Scroll'),0,{css:{'backgroundPosition':'-595px -525px'}},'+=.1');
tl.to($('.Scroll'),0,{css:{'backgroundPosition':'-517px -525px'}},'+=.1');
```
2013年9月24日 星期二
2013年6月26日 星期三
tinymce 4.0版外掛和主程式不同路徑怎麼解決
// 只要設定 window.tinymce.baseURL就能指定plugin的資料夾了
```javascript
window.tinymce.baseURL = '任意路徑'
```
2013年6月4日 星期二
simple_html_dom 轉 array
[simple_html_dom](http://simplehtmldom.sourceforge.net/)是一個很好用的 x(h)tml解析器
不過在使用上有些麻煩
所以就自己寫了將simple_html_dom轉array的function
xml檔案
```xml
<orderdoc>
<dochead>
<docno>999XXXyyyymmdd99</docno>
<docdate>2009-02-01</docdate>
<parentid>999</parentid>
</dochead>
<doccontent>
<order>
<eshopid>001</eshopid>
<opmode>A</opmode>
<eshoporderno>200906010001</eshoporderno>
<eshoporderdate>2009-02-01</eshoporderdate>
<servicetype>1</servicetype>
<shoppername>張小明</shoppername>
<shopperphone></shopperphone>
<shoppermobilphone></shoppermobilphone>
<shopperemail></shopperemail>
<receivername>張小明</receivername>
<receiverphone></receiverphone>
<receivermobilphone></receivermobilphone>
<receiveremail></receiveremail>
<receiveridnumber></receiveridnumber>
<orderamount>2000</orderamount>
<orderdetail>
<productid></productid>
<productname></productname>
<quantity></quantity>
<unit></unit>
<unitprice></unitprice>
</orderdetail>
<shipmentdetail>
<shipmentno>20000001</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>Y</LastShipment>
<shipmentamount>2000</shipmentamount>
<storeid>886751</storeid>
<eshoptype>04</eshoptype>
</shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</order>
<order>
<eshopid>001</eshopid>
<opmode>A</opmode>
<eshoporderno>200906010002</eshoporderno>
<eshoporderdate>2009-02-01</eshoporderdate>
廠商上線說明書
This Document is 7-Eleven /PIC/ Presco Confidential Page: 17
<servicetype>1</servicetype>
<shoppername>林志玲</shoppername>
<shopperphone></shopperphone>
<shoppermobilphone></shoppermobilphone>
<shopperemail></shopperemail>
<receivername>林志玲</receivername>
<receiverphone></receiverphone>
<receivermobilphone></receivermobilphone>
<receiveremail></receiveremail>
<receiveridnumber></receiveridnumber>
<orderamount>1200</orderamount>
<orderdetail>
<productid></productid>
<productname></productname>
<quantity></quantity>
<unit></unit>
<unitprice></unitprice>
</orderdetail>
<shipmentdetail>
<shipmentno>20000002</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>N</LastShipment>
<shipmentamount>800</shipmentamount>
<storeid>886750</storeid>
<eshoptype>04</eshoptype>
</shipmentdetail>
<shipmentdetail>
<shipmentno>20000003</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>Y</LastShipment>
<shipmentamount>400</shipmentamount>
<storeid>886750</storeid>
<eshoptype>04</eshoptype>
<shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</order>
</doccontent>
</orderdoc>
```
php檔案
```php
define("DOCROOT",dirname(__FILE__)."/");
require DOCROOT."simple_html_dom.php";
function domToArray($elements)
{
$results = array();
foreach ($elements as $element)
{
if (count($element->children) > 0)
{
$results[$element->tag][] = domToArray($element->children);
}
else
{
$results[$element->tag] = $element->text();
}
}
return $results;
}
$root = file_get_html(DOCROOT."test.xml");
$order_doms = domToArray($root->find("Order"));
print_r($order_doms);
```
2013年6月2日 星期日
php 民國年日期計算
西元年轉民國年,還要再處理日期的加減
光用php的date函數或是mktime是一件很麻煩的事情
所幸php內建了DataTime這個class來協助處理這件事情
```php
// 西元年轉民國年,並取得5天後的日期
$date = new DateTime("now");
$date->modify("+5 day");
$date->modify("-1911 year")
// 由於Y為四位數,所以利用ltrim去0
echo ltrim($date->format("Y-m-d H:i:s"),"0");
// 民國年轉西元年,並取得10天前的日期
$date = new DateTime("101-01-01");
$date->modify("+5 day");
$date->modify("+1911 year")
echo $date->format("Y-m-d H:i:s");
```
2013年2月5日 星期二
tempnam修改
有時候在寫程式的時候會用到暫存檔
一般我們可能都是用PHP內建的tempnam來建立暫存檔
但有時候用到某些library時,
會去認檔案的副檔名,但內建的tempnam所產出的暫存檔副檔名皆為 .tmp
所以就自己另外寫了一個程式來替代暫存檔的產生
```php
class File
{
public static function tempnam($dir = NULL,$suffix = "tmp"){
$name = strtoupper($suffix)."_".md5(time().mt_rand());
if (empty($dir) == TRUE){
$dir = sys_get_temp_dir();
}
$file = $dir.'/'.$name.".".$suffix;
$handle = fopen($file, "w");
fclose($handle);
if (realpath($file) == TRUE)
{
register_shutdown_function(create_function("", 'if (realpath("'.$file.'") == TRUE){unlink("'.$file.'");}'));
// register_shutdown_function(array("File", 'rmtempnam'),$file);
return $file;
}
throw new Exception();
}
} // End file
echo File::tempnam(NULL,"png");
// print xxxxxxx.png
```
2011年11月24日 星期四
php string 轉 boolean (php_filter)
PHP它是一個weak type的程式語言
所以只要字串有值,拿去做if判斷時,就一定會回傳true
但有時候會希望帶進來的參數為字串 "false" 時希望它的boolean為 false
在php5以前的版本可能只能這樣寫
```php
$bool = 'false';
$result = NULL;
if ($bool == 'false' OR !$bool){
$result = FALSE;
}
var_dump($result);
```
不過php5之後的版本可以換一個方式寫了
```php
$bool = filter_var($_GET['bool'], FILTER_VALIDATE_BOOLEAN);
```
這樣$_GET['bool']帶進來的值為 'false','no','null','0' 都會被轉為 FALSE
這個函式就像是JAVA的
```javascript
Boolean.parseBoolean(strBoolean);
```
如果要寫一些比較嚴謹的PHP程式用這個函式可能是比較好的作法...
PS. filter_var這個函式不只可以字串轉boolean、也可以驗證email、ip、url等等....
有興趣的話就上PHP官網查一查吧
2011年8月12日 星期五
php 單例設計模式
單例設計模式
今天在網路上看到的一個名詞
說穿了它就是例用static這個關鍵字
其實static早就php4就已經存在了
但很少看到討論它的功能
再順便討論一下吧
```php
function test()
{
static $a;
if ($a == FALSE)
{
$a = 1;
}
else
{
$a++;
}
echo $a;
}
test(); //輸出 1
test(); //輸出 2
test(); //輸出 3
test(); //輸出 4
/*
所以當你指定了變數為static時
它就不會因為function執行結束後而將變數回收
所以我們這個例用這個方法來實現單例設計模式
*/
class Test
{
function instance()
{
static $instance;
if ($instance == FALSE)
{
$instance = new Test();
}
return $instance;
}
}
$test =& Test::instance();
$test2 =& Test::instance();
$test3 =& Test::instance();
/*
這樣$test,$test2,$test3都會指向同一個物件
以上為PHP4的寫法
PHP4必須帶參考才能將物件指到同一下
那php5怎麼寫呢?
*/
class Test
{
static private $instance;
static public function instance()
{
if (self::$instance == FALSE)
{
self::$instance = new self;
}
return self::$instance;
}
}
$test = Test::instance();
/*
PHP5的CLASS支援這種寫法當然要用這種寫法了啊!
雖然採用PHP4的寫法也行....
但新的東西總是要用一下的是不是
*/
```
訂閱:
文章 (Atom)