vue中如何将js中的变量赋值给css以实现对样式的动态控制

JavaScript0104

vue中如何将js中的变量赋值给css以实现对样式的动态控制,第1张

方法一:

使用原生js操作dom的方法,来改变css的样式,比如

document.getElementById(id).style.property =newstyle

这里的new style 里面就可以使用js传入的变量。

此方法固然可以,但是对应改变一些复杂的css,比如动画什么的,操作起来就不怎么方便了。此时,如下的方法二就显得尤为重要了!

方法二:

利用css变量来处理,思路是将js变量赋值给css变量,然后在css样式中使用css变量。如下图所示,我们传入year变量,然后生成了--top、--bottom等变量,然后这些变量就可以在css中使用!

动态加载外部css样式及css样式,参考代码如下:

// 动态加载外部js文件

var flag = true

if( flag ){

loadScript( "js/index.js" )

}

function loadScript( url ){

var script = document.createElement( "script" )

script.type = "type/javascipt"

script.src = url

document.getElementsByTagName( "head" )[0].appendChild( script )

}

// 动态加载js

if( flag ){

var script = document.createElement( "script" )

script.type = "text/javascript"

script.text = " "

document.getElementsByTagName( "head" )[0].appendChild( script )

}

// 动态加载外部css样式

if( flag ){

loadCss( "css/base.css" )

}

function loadCss( url ){

var link = document.createElement( "link" )

link.type = "text/css"

link.rel = "stylesheet"

link.href = url

document.getElementsByTagName( "head" )[0].appendChild( link )

}

// 动态加载css样式

if( flag ){

var style = document.createElement( "style" )

style.type = "text/css"

document.getElementsByTagName( "head" )[0].appendChild( style )

var sheet = document.styleSheets[0]

insertRules( sheet,"#gaga1","background:#f00",0 )

}

function insertRules( sheet,selectorTxt,cssTxt,position ){

if( sheet.insertRule ){ // 判断非IE浏览器

sheet.insertRule( selectorTxt + "{" + cssTxt +"}" ,position )

}else if( sheet.addRule ){ //判断是否是IE浏览器

sheet.addRule( selectorTxt ,cssTxt ,position )

}

}

引入jquery

然后给你要设置动画的对象增加或者删除css3动画的类就可以了。

如我这里用colorchange这个渐变类在css里面写好动画效果以后在js里面给对象添加上就可以实现动画了

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title>Test</title>

    <style type="text/css">

        body{

            padding: 20px

            background-color:#FFF

        }

        .colorchange

        {

            animation:myfirst 5s

            -moz-animation:myfirst 5s /* Firefox */

            -webkit-animation:myfirst 5s /* Safari and Chrome */

            -o-animation:myfirst 5s /* Opera */

        }

        @keyframes myfirst

        {

            from {background:red}

            to {background:yellow}

        }

        @-moz-keyframes myfirst /* Firefox */

        {

            from {background:red}

            to {background:yellow}

        }

        @-webkit-keyframes myfirst /* Safari and Chrome */

        {

            from {background:red}

            to {background:yellow}

        }

        @-o-keyframes myfirst /* Opera */

        {

            from {background:red}

            to {background:yellow}

        }

        #main{

            width:100px

            height:100px

            background:red

        }

        #cgbt{

            width: 100px

            margin: 20px 0 0 0

            text-align: center

            cursor: pointer

        }

        #cgbt:hover{

            background-color: #2D93CA

        }

    </style>

</head>

<body>

<div id="main">

    我会变么?

</div>

<div id="cgbt">

    点我让上面的变颜色

</div>

<script src="jquery-3.2.1.min.js" type="application/javascript"></script>

<script>

    $(document).ready(function(){

        $("#cgbt").click(function(){

            $("#main").attr("class","colorchange")

        })

    })

</script>

</body>

</html>