根据屏幕大小切换CSS类

时间:2022-11-11 11:52:50

CSS nob here...

CSS nob在这里......

I'm looking at a responsive framework and imagining how I would accomplish different tasks.

我正在寻找一个响应式框架,并想象我将如何完成不同的任务。

Based on the size of the screen, they have classes added to the body tag such as:

根据屏幕的大小,它们将类添加到body标签中,例如:

.PhoneVisible, .DesktopVisible, etc...

.PhoneVisible,.DesktopVisible等...

They also have classes to make links into buttons :

他们还有类来链接到按钮:

.btn, small-button, med-button, large-button

.btn,小按钮,按钮,大按钮

I'm puzzled on how you would go about changing your CSS. I.E. something like:

我很困惑你会如何改变你的CSS。 I.E.就像是:

<a href="#" class="MyButtonOptions">XXXX</>

.PhoneVisible .MyButtonOptions { btn small-button }
.TabletVisible  .MyButtonOptions { btn  med-button }
.DesktopVisible .MyButtonOptions { btn large-button }

Do you have to set the varying options individually?

您是否必须单独设置不同的选项?

i.e. .PhoneVisible .MyButtonOptions { height:30px; } ???

即.PhoneVisible .MyButtonOptions {height:30px; } ???

All advice appreciated!

所有建议赞赏!

2 个解决方案

#1


11  

Take a look at this https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries.

看看这个https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries。

Another way is to attach the resize event some piece of "switch code".

另一种方法是将resize事件附加一些“切换代码”。

Something like this: http://jsfiddle.net/s5dvb/

像这样:http://jsfiddle.net/s5dvb/

HTML

HTML

<div id="body" class="limit400">
    <h1>Hey :D</h1>
</div>

CSS

CSS

.limit400 h1 { font-size:10px; }
.limit1200 h1 { font-size:50px; }

JS

JS

$(window).on('resize', function() {
    if($(window).height() > 400) {
        $('#body').addClass('limit1200');
        $('#body').removeClass('limit400');
    }else{
        $('#body').addClass('limit400');
        $('#body').removeClass('limit1200');
    }
})

About the frameworks, try http://purecss.io/ or http://getbootstrap.com/

关于框架,请尝试http://purecss.io/或http://getbootstrap.com/

Hope it helps.

希望能帮助到你。

#2


31  

CSS Media Queries are definetly the way to go.

CSS媒体查询绝对是最佳选择。

You can easily separate your CSS based upon the browser size, pixel density, etc.

您可以根据浏览器大小,像素密度等轻松分离CSS。

Here's a list of examples from CSS-Tricks.

以下是CSS-Tricks的示例列表。

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
    /* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
    /* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
    /* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
    /* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
    /* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
    /* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
    /* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
    /* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
    /* Styles */
}

#1


11  

Take a look at this https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries.

看看这个https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries。

Another way is to attach the resize event some piece of "switch code".

另一种方法是将resize事件附加一些“切换代码”。

Something like this: http://jsfiddle.net/s5dvb/

像这样:http://jsfiddle.net/s5dvb/

HTML

HTML

<div id="body" class="limit400">
    <h1>Hey :D</h1>
</div>

CSS

CSS

.limit400 h1 { font-size:10px; }
.limit1200 h1 { font-size:50px; }

JS

JS

$(window).on('resize', function() {
    if($(window).height() > 400) {
        $('#body').addClass('limit1200');
        $('#body').removeClass('limit400');
    }else{
        $('#body').addClass('limit400');
        $('#body').removeClass('limit1200');
    }
})

About the frameworks, try http://purecss.io/ or http://getbootstrap.com/

关于框架,请尝试http://purecss.io/或http://getbootstrap.com/

Hope it helps.

希望能帮助到你。

#2


31  

CSS Media Queries are definetly the way to go.

CSS媒体查询绝对是最佳选择。

You can easily separate your CSS based upon the browser size, pixel density, etc.

您可以根据浏览器大小,像素密度等轻松分离CSS。

Here's a list of examples from CSS-Tricks.

以下是CSS-Tricks的示例列表。

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
    /* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
    /* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
    /* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
    /* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
    /* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
    /* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
    /* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
    /* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
    /* Styles */
}