向下滚动并再次向上滚动后,灯箱顶部边距发生变化

发布于 2025-01-04 00:30:08 字数 1207 浏览 1 评论 0原文

我正在使用灯箱来显示带照片的评论。问题是,当我向下滚动查看灯箱并滚动回顶部时,即使在指定顶部后,上边距也会发生变化。

我指定顶部和左侧的 jQuery 代码是:

jQuery.fn.center = function() {
  this.css("position","fixed");
  var top = "20px";
  left = ( $(window).width() - this.width() ) / 2+$(window).scrollLeft()+"px";
  this.animate({top: top, left: left});
  return this;
}

$('#tp-lightboxactitem').center();  

这是灯箱 div 的 HTML:

<div class="dark-lightbox lightboxitem tp-lightboxactitem-loaded" id="tp-lightboxactitem" style="display: block; top: 20px; left: 314.5px; position: fixed; margin-top: 30px;">
  <div>
    <center>
      <img src="uploads/_DSC9322_watcopy_bor_5_$1$uh2.fw5.$uFln.yw5QjSgVKjmhD8.jpg?id=3&amp‌​;pic=113" class="tp-mainimage" id="plzxc">
    </center>
  </div>
  <div class="dark-lightbox infofield">
    <div class="dark-lightbox title"></div>
    <div class="dark-lightbox pageofformat">( 1 / 5 )</div>
    <div class="dark-lightbox description" id="dark-lightbox description">

滚动前如何显示

![滚动前如何显示][1]

滚动后如何显示向下然后再向上

![向下然后向上滚动后的样子][2]

I'm using a lightbox to display comments with photo. The problem is that when I scroll down to see the lightbox, and scroll back to the top, the top margin changes even after specifying the top.

My jQuery code to specify top and left is :

jQuery.fn.center = function() {
  this.css("position","fixed");
  var top = "20px";
  left = ( $(window).width() - this.width() ) / 2+$(window).scrollLeft()+"px";
  this.animate({top: top, left: left});
  return this;
}

$('#tp-lightboxactitem').center();  

Here is the HTML for the lightbox div:

<div class="dark-lightbox lightboxitem tp-lightboxactitem-loaded" id="tp-lightboxactitem" style="display: block; top: 20px; left: 314.5px; position: fixed; margin-top: 30px;">
  <div>
    <center>
      <img src="uploads/_DSC9322_watcopy_bor_5_$1$uh2.fw5.$uFln.yw5QjSgVKjmhD8.jpg?id=3&‌​;pic=113" class="tp-mainimage" id="plzxc">
    </center>
  </div>
  <div class="dark-lightbox infofield">
    <div class="dark-lightbox title"></div>
    <div class="dark-lightbox pageofformat">( 1 / 5 )</div>
    <div class="dark-lightbox description" id="dark-lightbox description">

How it appears before scrolling

![How it appears before scrolling][1]

How it appears after scrolling down and then up again

![How it appears after scrolling down and then up again][2]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2025-01-11 00:30:08

通过做一些小的改变来解决这个问题,

这里是代码,希望它能帮助有需要的人

cssAnimate({'left':($(window).width()/2 - thisw/2)+'px', 'top':'20px' },{duration:300,queue:false});

Got this fixed by doing small changes

here is the code hope it helps some one in need

cssAnimate({'left':($(window).width()/2 - thisw/2)+'px', 'top':'20px' },{duration:300,queue:false});
何以笙箫默 2025-01-11 00:30:08

尝试使用这样的东西。希望它能解决它。

$("#YourDivID").animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );

Try using something like this. Hope it resolves it.

$("#YourDivID").animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文