jQuery移动横向和肖像类

时间:2010-11-23 06:37:17

标签: jquery coding-style landscape portrait jquery-mobile

我已经开始使用jquery移动框架但我似乎无法使用横向和纵向类来缩小样式。

文件说

  

HTML元素将始终具有“纵向”或“横向”类,具体取决于浏览器或设备的方向。

所以我认为<h1>foo</h1>可能是<h1 class="landscape">foo</h1><h1 class="portrait">foo</h1>

h1.landscape { font-size:16px; }h1.portrait { font-size:9px; }似乎无效。

如果有人能够对此有所启发,我们将不胜感激。

6 个答案:

答案 0 :(得分:5)

确定。我决定看看最新情况,并使用curl通过android视图获取源代码。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.actwebdesigns.co.uk');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2');
$html = curl_exec($ch);

echo $html;

唯一具有横向或纵向类的元素是html标记。

<html xmlns="http://www.w3.org/1999/xhtml" class="ui-mobile landscape min-width-320px min-width-480px min-width-768px min-width-1024px"><head><meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1"></html>

我还注意到框架不会在旋转时自动切换类,因此我测试的以下代码可以正常工作。

<script type="text/javascript">
$(window).resize( function(){
    $('html').toggleClass('landscape, portrait');
});
</script>

废除上述有缺陷的内容。

<script type="text/javascript">
$(window).resize( function(){
    var height = $(window).height();
    var width = $(window).width(); 
    var ob = $('html');
    if( width > height ) {
        if( ob.hasClass('portrait') ) {
            ob.removeClass('portrait').addClass('landscape');
        }
    }else{
        if( ob.hasClass('landscape') ) {
            ob.removeClass('landscape').addClass('portrait');
        }
    }
});
</script>

使用Tommi Laukkanen's脚本中的liitle,上面的工作正常。

答案 1 :(得分:5)

抱歉,这已经过时了! 从HTML5到CSS3 MediaQueries。 现在您可以决定CSS中的样式:

@media screen and (orientation: landscape) {

 h1 {
  color: red;
 }

 #someId {
   width: 50%;
 }

}

@media screen and (orientation: portrait) {

 h1 {
  color: blue
 }

 #someId {
   width: 100%;
 }

}

答案 2 :(得分:3)

纵向和横向类被添加到html元素(不是页面上的每个元素),因此您希望css选择器首先查找横向/纵向。以下作品:

html.landscape h1 { font-size:16px; }
html.portrait h1 { font-size:9px; }

答案 3 :(得分:0)

将其放入lil插件

(function($){
    $.fn.portlandSwitch = function ( options ) {
        // redefine styles to either landscape or portrait on phone switch
        $(window).resize( function(){
            var height = $(window).height();
            var width = $(window).width(); 
            var ob = $('html');
            if( width > height ) {
                if( ob.hasClass('portrait') ) {
                    ob.removeClass('portrait').addClass('landscape');
                }
            }else{
                if( ob.hasClass('landscape') ) {
                    ob.removeClass('landscape').addClass('portrait');
                }
            }
        });
    }
})(jQuery);



$.portlandSwitch();

答案 4 :(得分:0)

使用此功能:

//Detect change rotation
    function doOnOrientationChange()
    {
        switch(window.orientation)
        {
            case -90:
            case 90:
                alert('landscape');
                $('body').addClass('landscape');
                $('body').removeClass('portrait');
                break;
            default:
                alert('portrait');
                $('body').addClass('portrait');
                $('body').removeClass('landscape');
                break;

        }
    }

答案 5 :(得分:0)

这是一个在不同设备上测试的完全可用的版本(基于Phil Jackson代码):)

我确信jQuery Mobile曾经处理过这个问题,但是我还有另一个基于屏幕方向的工作版本,不过我认为这会更好,因为它很简单......

if($(window).width() > $(window).height()){
    if($('body').hasClass('portrait')){
        $('body').removeClass('portrait').addClass('landscape');
    } else if(!$('body').hasClass('portrait')) {
        $('body').addClass('landscape');
    }
}
else {
    if($('body').hasClass('landscape')){
        $('body').removeClass('landscape').addClass('portrait');
    } else if(!$('body').hasClass('landscape')) {
        $('body').addClass('portrait');
    }
}

这会添加纵向或横向类,您无需将其硬编码到模板文件中:)

由于

相关问题