动态修改css 规则

页面引用了两个样式表:

<link href="css/mui.min.css" rel="stylesheet" />
<link href="css/new_menu.css" rel="stylesheet" />
//获取样式表对象
function getStyleSheet(){
    
    //获取样式表对象,此处为new_menu.css样式文件
    var styleSheets = document.styleSheets;
    
    for(var i=0; i<styleSheets.length;i++){
        
        var sheet=styleSheets[i];//console.log(sheet.href);
        
        if(sheet.href==undefined || sheet.href==null){
            continue;
        }
        
        if(sheet.href.indexOf('new_menu')>0){
            
            styleSheet=sheet;//console.log(i);
            break;
        }
    }
    //console.log(styleSheet);
}
for(var item in rule){
console.log(item);
}
输出样式规则对象属性
cssRules at new_touchMove.html:204
name at new_touchMove.html:204
parentRule at new_touchMove.html:204
parentStyleSheet at new_touchMove.html:204
cssText at new_touchMove.html:204
type at new_touchMove.html:204
insertRule at new_touchMove.html:204
deleteRule at new_touchMove.html:204
findRule at new_touchMove.html:204
UNKNOWN_RULE at new_touchMove.html:204
STYLE_RULE at new_touchMove.html:204
CHARSET_RULE at new_touchMove.html:204
IMPORT_RULE at new_touchMove.html:204
MEDIA_RULE at new_touchMove.html:204
FONT_FACE_RULE at new_touchMove.html:204
PAGE_RULE at new_touchMove.html:204
WEBKIT_KEYFRAMES_RULE at new_touchMove.html:204
WEBKIT_KEYFRAME_RULE at new_touchMove.html:204
SUPPORTS_RULE at new_touchMove.html:204
WEBKIT_FILTER_RULE at new_touchMove.html:204
HOST_RULE at new_touchMove.html:204

 

//动态创建css规则
function createRule(menuCss,i,x,y,offsetX,offsetY,cssIndex){
    
    var offset_x=x-offsetX;
    var offset_y=y-offsetY;
    
    var btn ='.btn'+i;
    var btnCss='left: '+offset_x+'px; top: '+offset_y+'px; animation: btn'+i+' 300ms;-webkit-animation: btn'+i+' 300ms;-moz-animation: btn'+i+' 300ms;-o-animation: btn'+i+' 300ms;';
    menuCss.insertRule(btn+'{'+btnCss+'}',cssIndex);
    
    var webkitKeyframes ='@-webkit-keyframes btn'+i;
    var webkitKeyframesCss='0%{ left: '+x+'px; top: '+y+'px; } 100%{ left: '+offset_x+'px; top: '+offset_y+'px;}';
    menuCss.insertRule(webkitKeyframes+'{'+webkitKeyframesCss+'}',cssIndex+1);
    
    var keyFrames ='@keyframes btn'+i;
    var keyFramesCss='0%{ left: '+x+'px; top: '+y+'px; } 100%{ left: '+offset_x+'px; top: '+offset_y+'px;}';
    menuCss.insertRule(keyFrames+'{'+keyFramesCss+'}',cssIndex+2);
    
    var mozKeyframes ='@-moz-keyframes btn'+i;
    var mozKeyframesCss='0%{ left: '+x+'px; top: '+y+'px; } 100%{ left: '+offset_x+'px; top: '+offset_y+'px;}';
    menuCss.insertRule(mozKeyframes+'{'+mozKeyframesCss+'}',cssIndex+3);
}

 

//删除css规则
function delRules(styleSheet){
    
    for(var i=21; i<styleSheet.cssRules.length; i++){
         var rule = styleSheet.cssRules[i];
         
         //rule.type == CSSRule.KEYFRAMES_RULE || rule.type == CSSRule.WEBKIT_KEYFRAMES_RULE || rule.type == CSSRule.MOZ_KEYFRAMES_RULE
         if(rule.type == 1 || rule.type == 7){ 
             //根据规则索引删除规则
             styleSheet.deleteRule(i);
         }
    }
}

 

转载于:https://www.cnblogs.com/wgx0428/p/9199323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值