简体   繁体   English

如何使用jQuery指定css并覆盖css文件中的css

[英]How to specify css using jquery and overwrite css in css file

I have a three containers with different width. 我有三个宽度不同的容器。 First container is taking 14% and second container is taking 26% and third container is taking 60%.Here is my code. 第一个容器占14%,第二个容器占26%,第三个容器占60%。这是我的代码。

 <div class="mainContainer">
    <div class="container1">
       <div>
            <label>Label1</label></div>
        </div>
        <div ">
            <label>Label2</label></div>
        </div>
        <div  ng-click="minPageType()" title="Toggle Panel">
            <div  ng-if="togglePanel">
                <i class="fa fa-angle-double-left"></i>
            </div>
            <div ng-if="!togglePanel">
                <i class="fa fa-angle-double-right"></i>
            </div>
        </div>
    </div>
    <div class="container2">             
         // Container2 items
    </div>
    <div class="container3">
      //Container3 contents
   </div>
 </div>   

Here is  my css

.container1{
  position: absolute; 
  width:14%;   
}  
.container2 {
  width: 26%;
  left: 14%;  
  position:absolute;
}

.container3 {
  width: 60%;
  left: 40%;  
  position:absolute;
}

It is working. 这是工作。 when the page open it shows three containers with proper width and when I click "fa fa-angle-double-left" icon the first container should have fixed with 56px and it should not overlap with other conatiners when I resize screen. 当页面打开时,它显示三个具有适当宽度的容器,并且当我单击“ fa fa-angle-double-left”图标时,第一个容器应固定为56px,并且在我调整屏幕大小时不应与其他色粉重叠。 I tried this jquery code. 我尝试了这个jQuery代码。

 $scope.minPageType = function(){
        if($scope.pageTypeToggle === true){
            //max screen size          
           $(".container1").css("width", "14%");
           $(".container2").css("left", "14%");
            $(".container3").css("left", "40%");
            $(".container3").css("width", "60%");
            $scope.togglePanel = true;
        } else {
            //min screen size
            $(".container1").css("width", "56px");
            $(".container2").css("left", "56px");
            $(".container3").css("left", "29%");
            $(".container3").css("width", "71%");
            $scope.togglePanel = false;
        }
        $scope.pageTypeToggle = !$scope.pageTypeToggle;
    };

This code is not working. 该代码不起作用。 when I put this code and I tried resize the screen and the first container width is not fixed to 56px.the percentages mentioned in css is overwriting . 当我放置此代码并尝试调整屏幕大小时,第一个容器宽度未固定为56px。css中提到的百分比被覆盖。 In console it still showing the first conatiner width is 14%. 在控制台中,它仍显示第一个着色器宽度为14%。 How to not override css mentioned using jquery with percentages specified for width in css file? 如何不使用在CSS文件中为宽度指定百分比的jquery覆盖提到的CSS?

This is easy and simple method to change the attribute of any tag using jquery. 这是使用jquery更改任何标签属性的简便方法。

Use attr() instand of css(). 使用attr()理解css()。

 $scope.minPageType = function(){
    if($scope.pageTypeToggle === true){
        //max screen size          
       $(".container1").attr('style','width:14% !important')
       $(".container2").attr('style','left:14% !important')
        $(".container3").attr('style','left:40% !important')
        $(".container3").attr('style','width:60% !important')
        $scope.togglePanel = true;
    } else {
        //min screen size
        $(".container1").attr('style','width:56px !important')
        $(".container2").attr('style','left:56px !important')
        $(".container3").attr('style','left:29% !important')
        $(".container3").attr('style','width:17% !important')
        $scope.togglePanel = false;
    }
    $scope.pageTypeToggle = !$scope.pageTypeToggle;
};

I hope you will get your solution. 希望您能找到解决方案。

First off, your markup is a bit jacked. 首先,您的标记有点麻烦。 There are too many closing div tags, and a stray opening double-quote. 结束div标签太多,并且有一个流浪的开头双引号。 Here's my take at what it should be: 这是我的看法:

 <div class="mainContainer">
    <div class="container1">
       <div>
            <label>Label1</label>
        </div>
        <div>
            <label>Label2</label>
        </div>
        <div  ng-click="minPageType()" title="Toggle Panel">
            <div  ng-if="togglePanel">
                <i class="fa fa-angle-double-left"></i>
            </div>
            <div ng-if="!togglePanel">
                <i class="fa fa-angle-double-right"></i>
            </div>
        </div>
    </div>

    <div class="container2">             
         // Container2 items
    </div>

    <div class="container3">
      //Container3 contents
   </div>
</div> 

Your JS seem to work in my demo: https://jsfiddle.net/y7tjeazo/1/ 您的JS似乎可以在我的演示中使用: https : //jsfiddle.net/y7tjeazo/1/

Here are some things to check: 这里有一些要检查的东西:

  1. Make sure jQuery is loaded 确保已加载jQuery
  2. Make sure the $scope class is initialized 确保$ scope类已初始化
  3. Make sure the $scope.minPageType() function is being called 确保正在调用$ scope.minPageType()函数
  4. Make sure all #3 occurs after the markup has already loaded 确保所有#3都在标记已加载之后发生

Lastly, since this is all presentational, consider using CSS + media queries rather than JS. 最后,由于这只是表示形式,因此请考虑使用CSS +媒体查询而不是JS。 It's much more maintainable. 它更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM