javascript - 如何防止图像在移动应用中滚动时移动
问题描述
我正在开发一个移动应用程序。我使用 JavaScript 作为代码和一些 jQuery mobile (1.4.5)。我的问题是,当我滚动时,我的图像会跟随滚动。我在网页版中没有这个问题,但在我的手机上,图像没有修复。当我在任何设备上滚动时,我希望我的图像能够固定在我想要的特定位置。让我简要解释一下我的代码:
- 当我打开我的应用程序时,一些图像出现在它们的位置。为此,我简单地使用:
<img src="js/image.jpg" style="position: absolute; top:105px; left:10px;" width="110" height="85">
效果很好,即使在移动应用程序中,此图像也会保留在原位。
- 然后,其他图片必须出现。为此,我使用此功能:
function im2(a,b,c,d){var x = document.createElement("IMG");
x.setAttribute("src", a);
x.setAttribute("style",b);
x.setAttribute("width", c);x.setAttribute("height",d );
document.body.appendChild(x);
}
然后调用图像:
<im2("js/image2.jpg","position: fixed; top:210px ; left:165px","280","35")>
然后当我滚动时,这个图像在移动设备上不再固定(在网络上工作正常)...... :-(
- 所以,我尝试使用这个 CSS 来粘贴图像,以使其无法移动
<style>
img.sticky {
position: -webkit-sticky;
position: sticky;
}
</style>
在 Javascript 中,我使用相同的函数 im2 :im2(w, etc){ x.setAttribute("class", w); etc}
然后,我这样调用函数:
im2("sticky","js/image2.jpg","position: fixed; top:210px ; left:165px","280","35")
和....没有什么坚持。
我认为这是因为 jqm 层给出了这个意想不到的结果。那么,我应该怎么做/写?
解决方案
由于一些未知的原因,它可以工作......图像保持固定..现在?
当我从一个页面滑动到另一个页面时,问题仍然存在。在另一篇文章中,我发现这个解决方案听起来很有希望。我不能直接与用户交谈或发表评论,因为我离我还有 2 小时的路程。这是应该工作的jQuery代码,但它不适用于我。我应该把这条线放在哪里?
$('img').on('dragstart', function(event) { event.preventDefault(); });
这是我使用的刷卡脚本:
<script>
$(document).on('swipeleft', '.ui-page', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var nextpage = $.mobile.activePage.next('[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {transition: "slide", reverse: false}, true, true);
}
event.handled = true;
}
return false;
});
$(document).on('swiperight', '.ui-page', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var prevpage = $(this).prev('[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {transition: "slide", reverse: true}, true, true);
}
event.handled = true;
}
return false;
});
推荐阅读
- azure - 将 Microsoft Bot Framework Bot 限制为特定 AD 租户
- node.js - 缓存服务方法的返回值
- python - 为什么 line.split('\t')[1] 不等于 0?
- c# - 根据需要验证正文中的不可为空属性 - AspNetCore 3.1
- regex - 带有反向引用的反向正则表达式
- heroku - 当我想在 Heroku 上托管我的 Discord.py 机器人但它给了我这个
- html - 角度图像加载
- matlab - 如何连接坐标并填充区域以创建二进制掩码?
- javascript - 无法在锚标记数据模板中传递 JSON 对象
- file - 如何保存 .zabw 文件(压缩的 abiword)?