mirror of https://github.com/layui/layui
新增 `lay.getStyleRules()` 方法及优化 `lay.style()`
parent
5c9a22f88e
commit
a57acc4f02
|
@ -111,32 +111,54 @@
|
||||||
return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);
|
return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 获取 style rules
|
||||||
|
lay.getStyleRules = function(style, callback) {
|
||||||
|
if (!style) return;
|
||||||
|
|
||||||
|
var sheet = style.sheet || style.styleSheet || {};
|
||||||
|
var rules = sheet.cssRules || sheet.rules;
|
||||||
|
|
||||||
|
if (typeof callback === 'function') {
|
||||||
|
layui.each(rules, function(i, item){
|
||||||
|
if (callback(item)) return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return rules;
|
||||||
|
};
|
||||||
|
|
||||||
// 创建 style 样式
|
// 创建 style 样式
|
||||||
lay.style = function(options){
|
lay.style = function(options){
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
var style = lay.elem('style');
|
var style = lay.elem('style');
|
||||||
var styleText = options.text || '';
|
var styleText = options.text || '';
|
||||||
var target = options.target || lay('body')[0];
|
var target = options.target;
|
||||||
|
|
||||||
if(!styleText) return;
|
if (!styleText) return;
|
||||||
|
|
||||||
// 添加样式
|
// 添加样式
|
||||||
if('styleSheet' in style){
|
if ('styleSheet' in style) {
|
||||||
style.setAttribute('type', 'text/css');
|
style.setAttribute('type', 'text/css');
|
||||||
style.styleSheet.cssText = styleText;
|
style.styleSheet.cssText = styleText;
|
||||||
} else {
|
} else {
|
||||||
style.innerHTML = styleText;
|
style.innerHTML = styleText;
|
||||||
}
|
}
|
||||||
|
|
||||||
lay.style.index = lay.style.index || 0;
|
// ID
|
||||||
lay.style.index++;
|
style.id = 'LAY-STYLE-'+ (options.id || function(index) {
|
||||||
|
lay.style.index++;
|
||||||
var id = style.id = 'LAY-STYLE-'+ (options.id || 'DF-'+ lay.style.index)
|
return 'DF-'+ index;
|
||||||
var styleElem = lay(target).find('#'+ id);
|
}(lay.style.index || 0));
|
||||||
|
|
||||||
styleElem[0] && styleElem.remove();
|
// 是否向目标容器中追加 style 元素
|
||||||
lay(target).append(style);
|
if (target) {
|
||||||
|
var styleElem = lay(target).find('#'+ style.id);
|
||||||
|
styleElem[0] && styleElem.remove();
|
||||||
|
lay(target).append(style);
|
||||||
|
}
|
||||||
|
|
||||||
|
return style;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 元素定位
|
// 元素定位
|
||||||
|
@ -467,4 +489,4 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}(window, window.document); // gulp build: lay-footer
|
}(window, window.document); // gulp build: lay-footer
|
||||||
|
|
Loading…
Reference in New Issue