2017-01-10 36 views
0

所以當用戶點擊一個按鈕時,我想打開一個彈出窗口,具體取決於頁面的寬度,我希望相應地調整popUp的大小。bootstrap調整頁面未找到視口

找到美味凌晨例如How to detect responsive breakpoints of Twitter Bootstrap 3 using JavaScript?

// Executes only in XS breakpoint 
if(viewport.is('xs')) { 
    // ... 
} 

// Executes in SM, MD and LG breakpoints 
if(viewport.is('>=sm')) { 
    // ... 
} 

// Executes in XS and SM breakpoints 
if(viewport.is('<md')) { 
    // ... 
} 

問題是視點射擊是沒有定義

Uncaught ReferenceError: viewport is not defined 
at <anonymous>:1:1 

,但我已經在我的腦海包括它

<!--Viewport--> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 

我也有將引導腳本從頭部移動到身體。

有沒有人有任何想法什麼是錯的? ta

+0

如果它是唯一的要求**「我想打開一個彈出窗口,取決於頁面的寬度,我想調整popUp的大小。」**不是定義CSS樣式彈出窗口寬度的解決方案在'%'或'vw'單位中? – Banzay

回答

1

我認爲他們的意思是「視口」作爲字符串,你應該傳遞給函數。

$(window).on('resize', cahngeViewport($(window).width())); 
 

 
function cahngeViewport(viewport) 
 
{ 
 
    if(viewport >= 1200) 
 
    { 
 
     console.log('lg'); 
 
    } 
 
    else if(viewport < 1200 && viewport > 768) 
 
    { 
 
    console.log('md'); 
 
    } 
 
    else if(viewport < 768 && viewport > 480) 
 
    { 
 
    console.log('sm'); 
 
    } 
 
    
 
    else if(viewport < 480) 
 
    { 
 
    console.log('xs'); 
 
    } 
 
} 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

,那麼你可以返回尺寸爲字符串,並檢查(viewport.is( 'MD'))
等..

希望它可以幫助你。 最好, 伊多。

+1

感謝您的幫助。將引用的文件移動到頁面的頂部而不是在body標籤內部解決了我的問題(即使示例說它們將它們放在body標籤內部.... anywho),您的答案確實有幫助......謝謝 – John

1

您需要在javascript中聲明viewport變量。

// Wrap IIFE around your code 
(function($, viewport){ 
    $(document).ready(function() { 

     // Executes only in XS breakpoint 
     if(viewport.is('xs')) { 
      // ... 
     } 

     // Executes in SM, MD and LG breakpoints 
     if(viewport.is('>=sm')) { 
      // ... 
     } 

     // Executes in XS and SM breakpoints 
     if(viewport.is('<md')) { 
      // ... 
     } 

     // Execute code each time window size changes 
     $(window).resize(
      viewport.changed(function() { 
       if(viewport.is('xs')) { 
        // ... 
       } 
      }) 
     ); 
    }); 
})(jQuery, ResponsiveBootstrapToolkit); 

視口isset到ResponsiveBootstrapToolkit所以你必須在項目中包含的responsive-bootstrap-toolkit

在這個例子中它使用IIFE做如下。

+0

感謝您的支持幫幫我。將引用的文件移動到頁面的頂部而不是在body標籤內解決了我的問題(儘管示例說它們將它們放在body標籤中,但是您的答案確實有幫助...謝謝 – John