移动Safari-iframe固定内容

时间:2019-02-28 11:39:38

标签: javascript ios css iframe mobile

移动Safari会强制将iframe调整为其内容大小。

因此,如果iframe中存在固定的div,则它不会修复。 是否有CSS属性可强制iframe在移动设备中滚动并尊重固定内容?

注意:固定内容位于iframe中,而不位于容器中,因此div滚动无法解决此问题。

<iframe src="page-with-fixed-content" style="width: 100%; height: 100%;">
    <!-- Fixed layer on the page loaded to iframe -->
    <div style="position: fixed; top:0;">
        Not Fixed on iPhone (Fixed on desktop)
    </div>
</iframe>

CodePan Example

Wanted vs actual behavior

5 个答案:

答案 0 :(得分:1)

几个月前,当我在使用Web应用程序时,我面临着这个挑战。经过一番研究后,下一篇文章中建议的“ Shadow DOM”方法有所帮助。

https://medium.com/@dvoytenko/amp-ios-scrolling-redo-2-the-shadow-wrapper-approach-experimental-3362ed3c2fa2

 var sd = document.body.attachShadow({mode: 'open'});

 // Main slot will absorb all undistributed children.
 var mainSlot = document.createElement('slot');
 var scroller = document.createElement('div');
 scroller.style = 'background: lightblue; position: absolute; top: 
  0; left: 0; right: 0; bottom: 0; overflow-x: hidden; overflow-y: auto; -webkit-overflow-scrolling: touch;';
  scroller.appendChild(mainSlot);
  sd.appendChild(scroller);

// Selectively, it's also possible to distribute fixed elements separately,
// emulating fixed layer transfer.
 var fixedLayer = document.createElement('div');
 fixedLayer.style = 'height: 0; width: 0; position: fixed; 
  overflow:visible;';
 sd.appendChild(fixedLayer);

 var mainFixedSlot = document.createElement('slot');
 mainFixedSlot.setAttribute('name', 'fixed');
 fixedLayer.appendChild(mainFixedSlot);

function addToFixedLayer(element) {
 //var slotId = String(Math.random());
 //var fixedSlot = document.createElement('slot');
 //fixedSlot.setAttribute('name', slotId);
 //fixedLayer.appendChild(fixedSlot);
 //element.setAttribute('slot', slotId);
 element.setAttribute('slot', 'fixed');
} 

 /*Call and pass fixed elemetns to addToFixedLayer method so that they stay 
 fixed while scrolling */

 addToFixedLayer(fixedDivId)

查看此演示https://jsfiddle.net/rsva63ep/

答案 1 :(得分:0)

如果iframe中有固定元素,则在查看内容时该元素将保持固定。 但是,媒体查询可能会更改CSS属性,从而在移动版本中不再固定该元素。

您可以使用boostrap导航栏尝试此操作。在PC版本中,导航栏是固定的,而在移动版本中,导航栏已不再固定。

PC版本:

enter image description here

移动版本:

enter image description here

您能告诉我们iframe中显示的网页代码吗?

答案 2 :(得分:0)

您的iframe是否固定?

iframe{
   position:fixed;
   top:0;
   right:0;
   bottom:0;
   left:0;
   width:100%;
   height:100%;
   overflow:auto;
}

答案 3 :(得分:0)

在iOS 5发行版中,据说MobileSafari支持固定位置的布局。

supported这个词需要带点盐,因为在接下来的文章中我打算向您展示各种问题。

请注意,我在iOS 5 Beta期间提交了许多此类错误-但上帝知道Radar Apple的工作原理,因此我不知道问题编号。 https://remysharp.com/2012/05/24/issues-with-position-fixed-scrolling-on-ios/ 如何解决?简单。搜索StackOverflow。之前曾问过这个问题:

position: fixed doesn't work on iPad and iPhone
Fixed positioning in Mobile Safari

答案 4 :(得分:0)

transform: translate3d(0,0,0);添加到固定位置元素。

div {
  position: fixed;
  transform: translate3d(0,0,0);
}
相关问题