GVKun编程网logo

jQuery计算窗口yScroll位置(jquery获取窗口宽度)

17

本文的目的是介绍jQuery计算窗口yScroll位置的详细情况,特别关注jquery获取窗口宽度的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解jQuery计算窗口

本文的目的是介绍jQuery计算窗口yScroll位置的详细情况,特别关注jquery获取窗口宽度的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解jQuery计算窗口yScroll位置的机会,同时也不会遗漏关于day17--1617章节 对jquery高度及位置操作scrolltop与jquery使用delegate绑定事件、javascript / jQuery中scroll和scrollTop之间的区别、javascript – jQuery scrollTo – 垂直中心窗口中的Div、javascript – jQuery计算图像背景位置顶部位置?的知识。

本文目录一览:

jQuery计算窗口yScroll位置(jquery获取窗口宽度)

jQuery计算窗口yScroll位置(jquery获取窗口宽度)

我正试图在窗口滚动时动态计算滚动条的位置.我可以使用以下命令获取加载时的初始滚动位置:

var scrollY = $(window).scrollTop();

但是当窗口滚动时这不会更新,我需要每次重新加载以获取更新的变量.当我滚动时,我需要做什么来保持这个值更新?我尝试过类似的东西:

$(document).scroll(function(e){
    $('#status').html(e.scrollY);
});

然后创建一个div和ID为’status’来输出结果,但我什么都没得到.任何人都可以协助吗?

谢谢,
克里斯

解决方法

为什么你认为当窗口滚动时scrollTop不会更新?当我尝试它,它工作得很好:

CSS:

#status { height: 1000px; padding: 100px; }

脚本:

$(document).scroll(function(e){
    $('#status').html($(window).scrollTop());
});

HTML:

<div id="status"></div>

http://jsfiddle.net/Z4sZp/

day17--1617章节 对jquery高度及位置操作scrolltop与jquery使用delegate绑定事件

day17--1617章节 对jquery高度及位置操作scrolltop与jquery使用delegate绑定事件

1、对jquery高度及位置操作

$(window).scrollTop()  获取位置
$(window).scrollTop(0) 设置位置
     <div id="i1"></div>
        <div>        <!-- overflow对div标签中的内容设置滚动条 -->
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
            <p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
        </div>
        <div id="i2"></div>
        <div></div>      <!-- 设置的高度大于当前页面的高度时,页面出现滚动条 -->
        <script src="jquery-1.12.4.js"></script>

在控制台中测试对scrolltop的应用:

$(window).scrolltop(),获取当前页面中滚动条的值;$(window).scrolltop(100),设置当前页面中滚动条的值;

$(‘div’).scrolltop(),获取当前div标签中滚动条的值;$(‘div’).scrolltop(100),设置当前div标签中滚动条的值;

2、offset获取坐标

offset().left:指定标签在html中的左坐标;offset().top:指定标签在html中的上坐标;

以1中的代码为例演示,添加i1与i2标签:如i1所示,默认是有8px的距离,设置margin为0,可以使边框为0

3、jquery使用delegate绑定事件

        <input id="t1" type="text"/>
        <input id="a1" type="button" value="添加" />
        
        <ul id="u1">
            <li>1</li>
            <li>2</li>
        </ul>
        <script src="jquery-1.12.4.js"></script>
        <script>
            $('#a1').click(function(){
                var v=$('#t1').val();
                var temp="<li>"+v+"</li>";
                $('#u1').append(temp);
            });
     </script>

现在这个代码可以实现的是,在输入框中输入值,点击添加按钮,可以添加在列表中;

现在要增加一个功能,点击每行列表时,可以将值通过弹窗的方式显示出来,可以通过以下三种方式的代码来实现:

       $('ul li').click(function () {
                var v = $(this).text();
                alert(v);
            })
            
            $('ul li').bind('click',function(){
                var v=$(this).text();
                alert(v);
            });
            
            $('ul li').on('click',function(){
                var v=$(this).text();
                alert(v);
            });

这三种方式点击页面现有的列表时,会弹出相应的弹窗;但是点击新增的列表时,没有出现;通过以下delegate来实现:

            $('ul').delegate('li','click',function(){
                var v=$(this).text();
                alert(v);
            });

 

javascript / jQuery中scroll和scrollTop之间的区别

javascript / jQuery中scroll和scrollTop之间的区别

有什么区别:

window.scroll(0,200);

$(window).scrollTop(200);

除了其中一个使用jQuery而另一个不使用jQuery之外,有什么区别?有人为卷轴设置动画而另一个没有吗?一个人会比另一个人工作得更快吗?

解决方法

scrollTop使用window.scrollTo,似乎: http://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.fn.scrollTop

性能方面,纯粹的js解决方案显然更快,但在大多数情况下它应该不重要:http://jsperf.com/js-vs-jquery-scroll

window.scroll和window.scrollTo之间的性能似乎没有任何差异.

javascript – jQuery scrollTo – 垂直中心窗口中的Div

javascript – jQuery scrollTo – 垂直中心窗口中的Div

我有一个网站使用页面顶部的固定菜单.

单击链接时,它应垂直滚动,以使该目标div的中心与窗口的垂直中心对齐,偏移标题的高度.
   – 这非常重要,无论显示器的分辨率如何,div都会居中

我正在使用jQuery和scrollTo,但无法弄清楚这需要的数学.

这是我的尝试:

function scrollTo(target) {
var offset;
var scrollSpeed = 600;

if (viewport()["width"] > 767 && !jQuery.browser.mobile) {
    // Offset anchor location and offset navigation bar if navigation is fixed
    offset = $(target).offset().top - document.getElementById('navigation').clientHeight;
} else {
    // Offset anchor location only since navigation bar is Now static
    offset = $(target).offset().top;
}

    $('html, body').animate({scrollTop:offset}, scrollSpeed);
}

解决方法:

最终想通了.只是对数学愚蠢.固定页眉和页脚的偏移也适用于所有分辨率.

    // scrollTo: Smooth scrolls to target id
function scrollTo(target) {
    var offset;
    var scrollSpeed = 600;
        var wheight = $(window).height();
        //var targetname = target;
        //var windowheight = $(window).height();
        //var pagecenterH = windowheight/2;
        //var targetheight = document.getElementById(targetname).offsetHeight;

    if (viewport()["width"] > 767 && !jQuery.browser.mobile) {
        // Offset anchor location and offset navigation bar if navigation is fixed
        //offset = $(target).offset().top - document.getElementById('navigation').clientHeight;
                offset = $(target).offset().top - $(window).height() / 2 + document.getElementById('navigation').clientHeight + document.getElementById('footer').clientHeight;
    } else {
        // Offset anchor location only since navigation bar is Now static
        offset = $(target).offset().top;
    }

    $('html, body').animate({scrollTop:offset}, scrollSpeed);
}

javascript – jQuery计算图像背景位置顶部位置?

javascript – jQuery计算图像背景位置顶部位置?

我将我的图像设置在CSS中心:

background-position: center center;

但是我想知道它在设置为中心后的顶级位置,我怎么能用jQuery或只是普通的javaScript来计算?

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-image: url('https://picsum.photos/g/200/600');
  height: 100%;
}

.tagline {
    font-size: 2.5vw;
    letter-spacing:0.05em;
    padding: 25% 25%;
}

.grid-x {
    border:1px solid red;
}

.cell {
    border: 1px solid blue;
}
<div>

  <div>
    <div>
      <div>
        <div>
          <p>Lorem ipsum dolor sit amet</p>
        </div>
      </div>
      <divhttps://www.jb51.cc/tag/ara/" target="_blank">arallax-viewport">
        <divhttps://www.jb51.cc/tag/ara/" target="_blank">arallax3">
        </div>
      </div>
    </div>
  </div>

</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.1/css/foundation.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.1/js/foundation.min.js"></script>

有任何想法吗?

解决方法:

正如我在上面评论的那样,我们需要找到公式,所以我们的想法是依赖于background-position:center和background-size:cover的逻辑.因此,由于图像将始终被拉伸,我们有两种可能性:

>图像的顶部将与div的顶部相同,因此top = 0
>图像的高度将比div长,顶部将被隐藏,因此顶部<0

enter image description here


你可能会注意到我认为相对于div顶部的顶部位置是我的起源

现在我们需要考虑图像如何覆盖div的所有不同情况,并根据每种情况计算图像的新高度.

这是一个代码示例:

//am using fixed values because they are kNown in this case, so this need to also be calculated
var him = 600;
var wim = 200;
var ratio = him / wim;
/*--*/

function get_top() {
  var h = $('.parallax3').height();
  var w = $('.parallax3').width();

  var diffh = h - him;
  var diffw = w - wim;
  if (diffw > 0 && diffh > 0) {
    //the image is smaller than the div so it should get bigger by at least the max difference
    if (diffh > diffw*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }
  } else if (diffw > 0) {
    //only the width if div is bigger so the image width will stretch and the height will be bigger and top < 0;
    var newH = (wim + diffw) * ratio;
    console.log((h - newH) / 2)
  } else if (diffh > 0) {
    //only the height is bigger so the image height will stretch and both heights will be equal to div and top = 0
    console.log(0);
  } else {
    // the image is bigger in this case so we do same logic as the start with abs value but we will reduce size so we consider the lowest value
    if (Math.abs(diffh) < Math.abs(diffw)*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will remain bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }

  }
}

get_top();

$(window).resize(function() {
  get_top()
})
body {
  height: 100vh;
}

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-image:url('https://picsum.photos/g/200/600');
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<divhttps://www.jb51.cc/tag/ara/" target="_blank">arallax3">
</div>

如果我们考虑在background-size中包含as值,则图像将始终包含在div中,因此两种可能性是:

>图像的顶部将与div的顶部相同,因此top = 0
>图像的高度较小,图像的顶部将是可见的,因此顶部> 0

enter image description here

与上述相同,我们需要考虑不同的情况并计算图像的新高度.

这是一个代码示例:

//am using fixed values because they are kNown in this case, so this need to also be calculated
var him = 600;
var wim = 200;
var ratio = him / wim;
/*--*/

function get_top() {
  var h = $('.parallax3').height();
  var w = $('.parallax3').width();
  var diffh = h - him;
  var diffw = w - wim;
  if (diffw > 0 && diffh > 0) {
    //the image is smaller than the div so it should get bigger by at least the min difference
    if (diffh < diffw*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }
  } else if (diffw > 0) {
    //only the width if div is bigger so the image height need to be reduced and both height will be equal
    console.log(0);

  } else if (diffh > 0) {
    //only the height is bigger so the image width will be reduced 
    var newH = (wim + diffw) * ratio;
    console.log((h - newH) / 2);
  } else {
    // the image is bigger in this case so we do same logic as the start with abs value but we will reduce size so we consider the biggest value
    if (Math.abs(diffh) > Math.abs(diffw)*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }

  }
}

get_top();

$(window).resize(function() {
  get_top()
})
body {
  height: 100vh;
}

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: contain;
  background-image: url('https://picsum.photos/g/200/600');
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<divhttps://www.jb51.cc/tag/ara/" target="_blank">arallax3">
</div>

两个代码段都可以进行优化以减少冗余,但我保留这些代码以更好地解释不同的情况.

我们今天的关于jQuery计算窗口yScroll位置jquery获取窗口宽度的分享就到这里,谢谢您的阅读,如果想了解更多关于day17--1617章节 对jquery高度及位置操作scrolltop与jquery使用delegate绑定事件、javascript / jQuery中scroll和scrollTop之间的区别、javascript – jQuery scrollTo – 垂直中心窗口中的Div、javascript – jQuery计算图像背景位置顶部位置?的相关信息,可以在本站进行搜索。

本文标签: