Jquery DIV滚动至浏览器顶部后固定不动代码

$(function(){
  //获取要定位元素距离浏览器顶部的距离
  var navH = $(".win").offset().top;
  //滚动条事件
  $(window).scroll(function(){
    //获取滚动条的滑动距离
    var scroH = $(this).scrollTop();
    //滚动条的滑动距离大于等于定位元素距离浏览器顶部的距离,就固定,反之就不固定
    if(scroH>=navH){
      $(".win").css({"position":"fixed","top":0});
    }else if(scroH<navH){
      $(".win").css({"position":"absolute","top":200,"right":70});
    }
  });
});

时间: 2024-08-05 21:07:56

Jquery DIV滚动至浏览器顶部后固定不动代码的相关文章

Jquery DIV滚动至浏览器顶部位置固定

获取元素(这里定位元素A)距离顶部的高度,接着设定scroll滚动的事件,比如超过那个高度,把A的位置设定为fixed,小于该高度,修改回relative. 方法一: $(function() { var elm = $('.nav'); var startPos = $(elm).offset().top; $.event.add(window, "scroll", function() { var p = $(window).scrollTop(); $(elm).css('pos

div滚动到页面顶端后固定住(转)

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>滚动至顶部后固定</title> <style type="text/css"> .wrapper{width:1000px;height:2000px;margin-left:auto;margin-right:auto;} .header{height:150p

div滚动到页面顶端后固定住

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>滚动至顶部后固定</title> <style type="text/css"> .wrapper{width:1000px;height:2000px;margin-left:auto;margin-right:auto;} .header{height:150p

JS 实现DIV 滚动至顶部后固定

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>DIV滚动至顶部后固定</title> </head> <body style="height:2000px;"> <div style="height: 200px"></div> <div id=&q

菜单滚动至顶部后固定

<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>菜单滚动至顶部后固定-柯乐义</title><style type="text/css">.wrapper{width:1000px;height:2000px;margin-left:auto;margin-right:auto;}.header{height:150px

获取DIV与浏览器顶部相聚一定位置之后移动DIV

获取元素(这里定位元素A)距离顶部的高度,接着设定scroll滚动的事件,比如超过那个高度,把A的位置设定为fixed,小于该高度,修改回relative. 方法一: $(function() { var elm = $('.nav'); var startPos = $(elm).offset().top; $.event.add(window, "scroll", function() { var p = $(window).scrollTop(); $(elm).css('pos

jQuery网页向下滚动导航固定顶部代码

<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQuery网页向下滚动导航固定顶部代码</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src=&

div固定在浏览器的最上方,不随滚动条滚动

1 #topDIV { 2 position: fixed; 3 top: 0; 4 z-index: 10000; 5 width: 100%; 6 height: 35px; 7 border-bottom: 1px solid #eee; 8 background-color: #f5f5f5; 9 } 把div固定在浏览器的最上方,不随滚动条滚动,类似百度.淘宝的上方.

jQuery 向上滚动屏幕 及 在高版本的 jQuery 浏览器判断

//向上移动 屏幕 //317这个高度可以通过$(document).scrollTop() 来测试得到 var userAgent = navigator.userAgent.toLowerCase(); //不能使用:$.browser.mozilla 之类的来判断.赋值也不能用他们,也就是不能写成: // $.browser.mozilla = /firefox/.test(userAgent); var isMozilla = /firefox/.test(userAgent); var