【写一个自己的js库】 5.添加修改样式的方法
1.根据id或class或tag修改样式,样式名是-连接格式的。
function setStyleById(elem, styles){
if(!(elem = $(elem)) return false;
for(prop in styles){
if(!styles.hasOwnProperty(prop)) continue;
if(elem.style.setProperty){
elem.style.setProperty(prop, styles[prop]);
}else{
elem.style[camelize(prop)] = styles[prop];
}
}
return true;
}
Lily['setStyleById'] = setStyleById;
function setStyleByClass(parent, tag, className, styles){
var elements = getElementsByClassName(className, tag, parent);
for(var i = 0; i < elements.length; i++){
setStyleById(elements[i], styles);
}
return true;
}
Lily['setStyleByClass'] = setStyleByClass;
function setStyleByTag(tag, styles, parent){
parent = $(parent) || document;
var elements = parent.getElementsByTagName(tag);
for(var i = 0; i < elements.length; i++){
setStyleById(elements[i], styles);
}
return true;
}
Lily['setStyleByTag'] = setStyleByTag;
2.获取class数组
function getClassNames(elem){
if(!(elem = $(elem))) return false;
return elem.className.split(/\s+/);
}
Lily['getClassNames'] = getClassNames;
3.判断是否有class
function hasClassName(elem, className){
if(!(elem = $(elem))) return false;
var classNames = getClassNames(elem);
for (var i = 0; i < classNames.length; i++) {
if(classNames[i] === className) return true;
}
return false;
}
Lily['hasClassName'] = hasClassName;
4.添加一个class
function addClass(elem, className){
if(!(elem = $(elem))) return false;
elem.className += (elem.className? ' ' : '') + className;
return true;
}
Lily['addClass'] = addClass;
5.删除一个class
function removeClassName(elem, className){
if(!(elem = $(elem))) return false;
var classNames = getClassNames(elem);
var length = classNames.length;
for (var i = length - 1; i >= 0; i--) {
if(classNames[i] === className) delete classNames[i];
};
elem.className = classNames.join(' ');
return (length == classNames.length) ? false : true;
}
Lily['removeClassName'] = removeClassName;
6.取得一个元素的计算样式
function getComputedStyle(elem, property){
if(!(elem = $(elem)) || !property) return false;
var value = elem.style[camelize(property)];
if(!value){
if(document.defaultView && document.defaultView.getComputedStyle){
var css = document.defaultView.getComputedStyle(elem, null);
value = css ? css.getPropertyValue(property) : null;
}else if(elem.currentStyle){
value = elem.currentStyle[camelize(property)];
}
}
return value == "auto" ? '' : value;
}
Lily['getComputedStyle'] = getComputedStyle;
7.渐变动画
function fadeColor(from, to, callback, duration, framesPerSecond){
function doTimeout(color, frame){
setTimeout(function (){
callback(color);
}, (duration * 1000 / framesPerSecond) * frame);
}
duration = duration || 1;
framesPerSecond = framesPerSecond || 15 * duration;
var frame = 1;
var r, g, b;
doTimeout('rgb(' + from.r + ',' + from.g + ',' + from.b + ')', 0);
while(frame < framesPerSecond + 1){
r = Math.ceil(from.r * (framesPerSecond - frame)/framesPerSecond + to.r * frame/framesPerSecond);
g = Math.ceil(from.g * (framesPerSecond - frame)/framesPerSecond + to.g * frame/framesPerSecond);
b = Math.ceil(from.b * (framesPerSecond - frame)/framesPerSecond + to.b * frame/framesPerSecond);
doTimeout('rgb(' + r + ',' + g + ',' + b + ')', frame++);
}
}
例子
Lily.addEvent(window, "load", function (){
fadeColor({'r': 255, 'g': 255, 'b': 255}, {'r': 0, 'g': 0, 'b': 0}, function (color){
document.body.style.backgroundColor = color;
}, 10, 50);
});
【写一个自己的js库】 5.添加修改样式的方法的更多相关文章
- 总结:js中4类修改样式的方法
前言 最近在写一个扩展右键菜单的插件,既然是插件,想着一步到位,把相关的style样式设置都丢进js文件中,直接加载一个js文件便可以使用该插件,所以今天就研究了下js批量的插入样式的方法,即addS ...
- 【写一个自己的js库】 3.添加几个处理字符串的方法
1.生成重复的字符串 if(!String.repeat){ String.prototype.repeat = function (count){ return new Array(count + ...
- 【写一个自己的js库】 2.实现自己的调试日志
还是本着学习的目的,实现一个自己的调试日志,界面很简单,就是将调试信息显示在页面的正中央,用一个ul包裹,每条信息就是一个li. 1.新建一个myLogger.js文件,将需要的方法声明一下.其中va ...
- 【写一个自己的js库】 1.搭个架子先
最近在看<javascript dom 高级程序设计>,想着跟着里面的代码敲一遍吧,也算是做一下学习笔记吧,所以这不是重新发明轮子,只是个学习的过程. 1.先确定自己的命名空间,并且加入几 ...
- 【写一个自己的js库】 4.完善跨浏览器事件操作
1.阻止冒泡. function stopPropagation(event){ event = event || getEvent(event); if(event.stopPropagation) ...
- 仿照jquery封装一个自己的js库(一)
所谓造轮子的好处就是复习知识点,加深对原版jquery的理解. 本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包 ...
- 仿照jquery封装一个自己的js库
所谓造轮子的好处就是复习知识点,加深对原版jquery的理解.本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包括 ...
- Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca
#34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...
- MySql表、字段、库的字符集修改及查看方法
这篇文章主要介绍了MySql表.字段.库的字符集修改及查看方法,本文分别给们它的修改及查看语句,需要的朋友可以参考下 修改数据库字符集: 代码如下: ALTER DATABASE db_name DE ...
随机推荐
- bzoj1630 [Usaco2007 Demo]Ant Counting
Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...
- Word Ladder II 解答
Question Given two words (beginWord and endWord), and a dictionary's word list, find all shortest tr ...
- HDU1754(线段树)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【HDU 4547 CD操作】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 题意:模拟DOS下的cd命令,给出n个节点的目录树以及m次查询,每个查询包含一个当前目录cur和 ...
- [置顶] 九度笔记之 1434:今年暑假不AC
题目1434:今年暑假不AC 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:307 解决:180 题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@# ...
- SSR分子标记
参考: SSR标记 分子标记开发与筛选之SSR SSR 分子标记开发策略及评价 SSR分子标记在牡丹亲缘关系研究中的应用与研究进展 SSR(Simple Sequence Repeats)标记是近年来 ...
- #include<string.h>
#include<string.h> 1 strcpy #include <string.h> char *strcpy(char *str1, const char *str ...
- ubuntu中文实训手册
http://people.ubuntu.com/~happyaron/udc-cn/lucid-html/ http://www.apachefriends.org/zh_cn/xampp-linu ...
- GCD 和延时调用
因为 Playground 不进行特别配置的话是无法在线程中进行调度的,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. GCD 是一种非常 ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...