使用iframe实现在pc端预览移动端页面的效果

 


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
  5. <script language="JavaScript" type="text/JavaScript">
  6. /**
  7. 弹出iframe页面(iframe后面会添加灰色蒙版)
  8. **/
  9. function showIframe(url){
  10.     $("<div id='showMobilePreview'>" +
  11.             "<div class='mobile_preview_header'><i class='mobile_preview_header_icon'></i></div>" +
  12.             "<div class='mobile_preview_frame'><iframe id='YuFrameMobilePreview' name='YuFrameMobilePreview' ></iframe></div>" +
  13.             "<div class='mobile_preview_footer'><i class='mobile_preview_footer_icon'></i></div>" +
  14.         "</div>").prependTo('body');  
  15.    
  16.      $("#YuFrameMobilePreview").attr("src", url);  
  17.   //添加背景遮罩
  18.    $("<div id='YuFrameMobilePreviewBg' style='cursor:pointer;width:100%;height:100%;background-color: Gray;display:block;z-index:9998;position:absolute;left:0px;top:0px;filter:Alpha(Opacity=30);/* IE */-moz-opacity:0.4;/* Moz + FF */opacity: 0.4; '/>").prependTo('body'); 
  19.     //点击背景遮罩移除iframe和背景
  20.     $("#YuFrameMobilePreviewBg").click(function() {
  21.         $("#showMobilePreview").remove();
  22.         $("#YuFrameMobilePreviewBg").remove();
  23.     });
  24. }
  25. </script>
  26. <style type="text/css">
  27. #showMobilePreview {
  28. z-index: 9999;
  29. width: 391px;
  30. height: 768px;
  31. position: absolute;
  32. left: 50%;
  33. top: 50%;
  34. transform: translate(-50%, -50%);
  35. opacity: 1;
  36. text-align: center;
  37. }
  38. .mobile_preview_header {
  39. display: block;
  40. position: absolute;
  41. top: 0;
  42. left: 0;
  43. height: 40px;
  44. width: 387px;
  45. background: #eeeff2;
  46. text-align: center;
  47. line-height: 40px;
  48. border-top-right-radius: 50px;
  49. border-top-left-radius: 50px;
  50. }
  51. .mobile_preview_header_icon {
  52. display: inline-block;
  53. width: 65px;
  54. height: 10px;
  55. background: #c8c9cc;
  56. border-radius: 9px;
  57. vertical-align: middle;
  58. margin-top: 18px;
  59. }
  60. .mobile_preview_frame {
  61. width: 375px;
  62. min-height: 294px;
  63. height: 667px;
  64. max-height: calc(100vh - 166px);
  65. top: 40px;
  66. left: 0;
  67. border: 6px solid #eeeff2;
  68. position: relative;
  69. background-color: #fff;
  70. display: block;
  71. }
  72. #YuFrameMobilePreview {
  73. border: none;
  74. width: 375px;
  75. height: 100%;
  76. }
  77. .mobile_preview_footer {
  78. display: block;
  79. position: absolute;
  80. bottom: 0;
  81. left: 0;
  82. height: 52px;
  83. width: 387px;
  84. background: #eeeff2;
  85. text-align: center;
  86. line-height: 45px;
  87. border-bottom-right-radius: 50px;
  88. border-bottom-left-radius: 50px;
  89. }
  90. .mobile_preview_footer_icon {
  91. display: inline-block;
  92. width: 43px;
  93. height: 43px;
  94. background: #c8c9cc;
  95. border-radius: 50%;
  96. vertical-align: middle;
  97. }
  98. </style>
  99. </head>
  100. <body>
  101. <input type="button" onClick="showIframe('https://m.baidu.com')" value="加载" />
  102. </body>
  103. <html>

 

文章来源: albertyang.blog.csdn.net,作者:Albert Yang,版权归原作者所有,如需转载,请联系作者。

原文链接:albertyang.blog.csdn.net/article/details/98623159

(完)