2016-09-25 232 views
2

我想設計一個網頁,其中的頁面內容顯示在iPhone屏幕內,但我無法讓溢出正常工作,因爲所有溢出都顯示出來。滾動時隱藏溢出

function getWidth() { 
 
    if (self.innerWidth) { 
 
    return self.innerWidth; 
 
    } 
 

 
    if (document.documentElement && document.documentElement.clientWidth) { 
 
    return document.documentElement.clientWidth; 
 
    } 
 

 
    if (document.body) { 
 
    return document.body.clientWidth; 
 
    } 
 
} 
 

 
function getHeight() { 
 
    if (self.innerHeight) { 
 
    return self.innerHeight; 
 
    } 
 

 
    if (document.documentElement && document.documentElement.clientHeight) { 
 
    return document.documentElement.clientHeight; 
 
    } 
 

 
    if (document.body) { 
 
    return document.body.clientHeight; 
 
    } 
 
} 
 

 
function setScreen() { 
 
    var img = document.getElementById('iphone'); 
 
    //or however you get a handle to the IMG 
 
    var myWidth = getWidth(); 
 
    var width = String(Math.round(getWidth()/2 - img.clientWidth/2 + (img.clientWidth/525) * 94)) + 'px'; 
 
    var height = String(Math.round(getHeight()/2 - img.clientHeight/2 + (img.clientWidth/915) * 170)) + 'px'; 
 
    document.getElementById("screen").style.paddingTop = height; 
 
    document.getElementById("screen").style.paddingLeft = width; 
 
    document.getElementById("screen").style.paddingRight = width; 
 
    document.getElementById("screen").style.paddingBottom = height; 
 
    console.log("Image Width: " + img.clientWidth); 
 
    console.log("Screen Width: " + myWidth); 
 
    console.log("Calculated PaddingLeft: " + width); 
 
} 
 

 
window.onresize = function() { 
 
    setScreen(); 
 
} 
 
setScreen();
.screen { 
 
    padding-top: 15%; 
 
    padding-left: calc(50% - 262px); 
 
    position: absolute; 
 
    overflow: hidden; 
 
    display: block; 
 
} 
 
.phone-image { 
 
    position: fixed; 
 
    top: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    margin: auto; 
 
    display: block; 
 
}
<img src='http://s11.postimg.org/vcchvwjub/iphone.jpg' id='iphone' class="phone-image"> 
 
<div id="screen" class="screen"> 
 
    <p>This is where my text goes and it should now wrap and only show within the phone, I think.</p> 
 
</div>

我已經把它放在一個jsfiddle,並希望知道我有錯。

非常感謝

+0

你的意思是這樣的結果https://jsfiddle.net/m2tsw830/2/? –

+0

不,我的意思是不要在iPhone屏幕外顯示文字,但是當我滾動文字顯示上方和下方時 – HenryM

回答

1

也許它的工作適合你。

你需要一個容器來放置你的文字和圖片,並玩相對和絕對的位置。

記得要隱藏滾動條,他的寬度在瀏覽器之間不同,你不希望文本容器改變其寬度。

::-webkit-scrollbar { 
 
    display: none; 
 
} 
 
body { 
 
    font-size: 100%; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 
p { 
 
    font-size: 0.8em; 
 
} 
 
#container { 
 
    position: relative; 
 
    width: 50%; 
 
    margin: 0 auto; 
 
} 
 
.screen { 
 
    overflow: scroll; 
 
    width: 60%; 
 
    height: 61%; 
 
    position: absolute; 
 
    top: 14%; 
 
    left: 20%; 
 
} 
 
.phone-image { 
 
    width: 100%; 
 
    display: block; 
 
}
<div id="container"> 
 
    <img src='http://s11.postimg.org/vcchvwjub/iphone.jpg' id='iphone' class="phone-image"> 
 
    <div id="screen" class="screen"> 
 
    <p>This is where my text goes and it should now wrap and only show within the phone, I think.</p> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum purus id nisl tristique condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nullam elementum dui et turpis euismod ullamcorper. 
 
     Maecenas id tempus lectus. Duis rutrum lectus ac diam scelerisque posuere. Praesent sit amet auctor tellus, id vestibulum massa. Sed sed leo et nisl lobortis imperdiet eget sed leo.</p> 
 
    <p>Nunc augue risus, pharetra in nulla ut, posuere finibus ipsum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse mollis erat mi, vel commodo lorem gravida in. Nulla euismod commodo leo, a egestas dolor 
 
     lacinia a. Suspendisse mattis lacus nisi, at consectetur ex auctor sed. Suspendisse potenti. Nunc hendrerit id nisl scelerisque cursus. Etiam suscipit, ipsum mollis volutpat ultricies, turpis arcu feugiat erat, et vulputate mauris velit vel leo. 
 
     Aenean ac dolor tempor, sodales nunc consectetur, gravida diam. Nulla sapien ipsum, tempus at vestibulum a, lobortis ut diam. Nam sit amet risus laoreet, mattis ex sed, semper odio. Nam sollicitudin tellus erat, ut vulputate elit bibendum ut.</p> 
 
    <p>Integer ultricies non nisi sed ultrices. Aliquam at odio accumsan, tincidunt ex nec, imperdiet magna. Vestibulum ac placerat justo. Nam facilisis tortor in tristique tincidunt. Phasellus tempor lectus eu libero dignissim, nec ultricies leo auctor. 
 
     Etiam dui tortor, faucibus id suscipit vel, aliquam non mi. Nulla non ultricies odio. Aenean at metus erat. Pellentesque eget consectetur velit, blandit pellentesque est. Nulla porta mi ligula, sit amet consequat lorem placerat ut. Ut mattis hendrerit 
 
     ex, at dapibus enim tincidunt et. Aenean quis nisl at neque porta sagittis sit amet nec mauris. Nullam libero purus, aliquet eu eleifend eget, fermentum a enim.</p> 
 
    </div> 
 
</div>

My Fiddle