2010年02月4日 – 15:06
By FoxLing Posted in JS/AJAX/DOM
需要实现:在光标位置处插入文字,以及获取焦点后光标位置处于文字末尾。
以前一直没有做过,今天了解了一下后实现并记录在此。
各浏览器TextArea获得焦点后的光标位置情况:
textarea.focus()
- FireFox: 所有文字结束处
- IE: 文字开头
- Opera: 文字开头
- Chrome: 文字开头
- Safari: 文字开头
IE支持document.selection
Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性
针对浏览器的特性进行判断并实现,代码如下:
function insertText(obj,str) {
obj.focus();
if (document.selection) {
var sel = document.selection.createRange();
sel.text = str;
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
var startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
function moveEnd(obj){
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character',len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}
Demo猛击这里
关于IE TextRange 对象的方法与属性介绍:
http://msdn.microsoft.com/zh-cn/library/ms535872%28en-us,VS.85%29.aspx
TextArea对象的属性:
https://developer.mozilla.org/en/DOM/TextArea
2009年11月25日 – 21:35
By FoxLing Posted in JS/AJAX/DOM
示例
解决方法
直接看代码吧:
function Dialog(){
this.panel = document.createElement('div');
this.panel.id = 'bmPanel';
this.panel.style.position = 'absolute';
this.panel.style.right = '0px';
this.panel.style.right = this.scrollTop() + 'px';
this.panel.style.zIndex = 100000;
this.panel.style.margin = '10px';
this.panel.style.width = '200px';
this.panel.style.height = '200px';
this.panel.style.border = '5px solid #CCC';
this.panel.innerHTML = '<iframe id="bmIframe" '
+ 'token="1" onload="c = this.getAttribute(\'token\'); if (c==3) {this.parentNode.parentNode.removeChild(this.parentNode)} this.setAttribute(\'token\',++c);"'
+ 'name="bmIframe" src="main.html" scrolling="no" frameborder="0" style="width:100%; height:100%; '
+ 'border:1px; padding:0px; margin:0px"></iframe>';
}
Dialog.prototype.show = function(){
document.body.appendChild(this.panel);
}
Dialog.prototype.scrollTop = function (){
return document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
}
注意iframe这一行:
onload="c = this.getAttribute(\'token\'); if (c==3) {this.parentNode.parentNode.removeChild(this.parentNode)} this.setAttribute(\'token\',++c);"
iframe里的关闭按钮链接一个简单的页面,通过不停地提交表单触发load事件,让token累加:
<body onload="document.getElementById('close').submit()">
<form id="close" action="closeing.html" method="get"></form>
</body>
2009年10月18日 – 23:32
By FoxLing Posted in AS/Flex/AIR
...
<param name="FlashVars" value="tag=as3"/>
<embed flashvars="tag=as3"/>
...
AS项目,或者Flash里:
stage.loaderInfo.parameters.tag
Flex3项目:
Application.application.parameters.tag
Flex4项目:
FlexGlobals.topLevelApplication.parameters.tag
备忘.
2009年09月3日 – 20:45
By FoxLing Posted in AS/Flex/AIR
这两天突然好玩做了这么个玩意,输入火兔网的帐号即可浏览该用户的所有照片。
地址:
http://app.foxling.cn/picturelife/
功能:
- 鼠标拖动:左右滑动,向左滑动时,自动加载更多的照片(前提是还有更多照片…)
- 鼠标点击空白区域:缩放到合适大小
- 鼠标滚轮:放大,缩小显示区域
- 鼠标点击单张小图:加载该大图,并且放大到原始尺寸。
预览图:

预览图1

预览图2
后续:
仓促弄出来玩玩,有待继续改进。
2009年07月6日 – 23:06
By FoxLing Posted in AS/Flex/AIR
通过自定义类的方式创建皮肤,控制性更强,占用的内存更少。
ProgrammaticSkin类是外观元素的基类,它们通过编程方式来绘制自身。
关于ProgrammaticSkin类的更多信息,猛击这里:
http://www.airia.cn/ActionScript3API/livedocs/mx/skins/ProgrammaticSkin.html
自定义类
package skins
{
import flash.display.GradientType;
import flash.display.Graphics;
import mx.skins.ProgrammaticSkin;
public class CustomBG extends ProgrammaticSkin
{
public function CustomBG()
{
super();
}
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
var g:Graphics = graphics;
var fillColors:Array = [0x191919,0x262626,0x191919];
var fillAlphas:Array = [1.0,1.0,1.0];
var fillRatios:Array = [0,127,255]
g.clear();
drawRoundRect(0, 0, w, h, 0, fillColors, fillAlphas,
horizontalGradientMatrix(0, 0, w, h), GradientType.LINEAR,fillRatios );
}
override public function get measuredWidth():Number
{
return 8;
}
override public function get measuredHeight():Number
{
return 8;
}
}
}
在CSS中使用ClassReference调用自定义类
.customBg
{
backgroundSize: “100%”;
backgroundImage: ClassReference(“skins.CustomBG”);
}