当前位置:首页 > 代码 > 正文

div当滚动到页面顶部的时候固定在顶部实例代码(js指定div滚动到顶部)

admin 发布:2022-12-19 16:12 119


本篇文章给大家谈谈div当滚动到页面顶部的时候固定在顶部实例代码,以及js指定div滚动到顶部对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

DiV+css,滚动条到特定位置时div一直停留顶部,滚动条在顶部时DIV在原位,求代码

单凭DIV+CSS恐怕不行,你要是会JQ可以这么干:

//jq部分

$(document).scroll(function(e) {

if($(document).scrollTop()!=0){

$("#test2").stop();

     $("#test2").animate({"top":"0"});

}

else{

$("#test2").stop();

$("#test2").animate({"top":"20px"});

}

});

!-- HTML部分 --

div id="test2"/div

/* CSS部分 */

#test2 { position:fixed; top:20px; background:#F30; }

js 网页代码 div随滚动条 移动到浏览器顶部 固定的问题

//html

body  

div style="background:#003366;height:500px;"空div/div 

div  class="purple" style="height:500px;width:200px;background:purple"

div class="nb" style="height:100px;width:100px;background:#9966FF"移动到顶部固定不变/div  

/div

div class="red" style="background:#ff0000;height:1500px;"空div/div  

div style="background:#ccc;height:1500px;"空div/div  

div class="nb2" style="height:100px;width:100px;background:gray;"移动到底部固定不变/div 

/body 

//js

$(function(){  

    //获取要定位元素距离浏览器顶部的距离 

    var navH = $(".nb").offset().top;  

    var navH2 = $(".nb2").offset().top; 

    var Pur= $(".purple").offset().top;     //紫色块

    var Red= $(".red").offset().top;     //红色块

    //滚动条事件  

    $(window).scroll(function(){  

      //获取滚动条的滑动距离  

      var scroH = $(this).scrollTop();  

      //滚动条的滑动距离大于等于定位元素距离浏览器顶部的距离,就固定,反之就不固定  

      if(scroH=navH){  

        $(".nb").css({"position":"fixed","top":0});  

      }else if(scroHnavH){  

        $(".nb").css({"position":"static"});    

      }

      if(scroH = navH2){  

        $(".nb2").css({"position":"fixed","bottom":"15px","right":"15px"});  

      }else if(scroHnavH){  

        $(".nb2").css({"position":"static"});    

      }

      if(scroH = Pur  scroH = Red){

        $(".nb").css({"position":"static"});  //在紫色块的时候不固定

        $(".nb2").css({"position":"static"});  //在紫色块的时候不固定

      }

    })  

  })

使用js或jq控制一个div,当滚动到页面顶部的时候固定在顶部,离开可继续滚动吗?

代码:

•$(function(){

•//获取要定位元素距离浏览器顶部的距离

•var navH = $(".nav").offset().top;

•//滚动条事件

•$(window).scroll(function(){

•//获取滚动条的滑动距离

•var scroH = $(this).scrollTop();

•//滚动条的滑动距离大于等于定位元素距离浏览器顶部的距离,就固定,反之就不固定

•if(scroH=navH){

•$(".nav").css({"position":"fixed","top":0});

•}else if(scroHnavH){

•$(".nav").css({"position":"static"});

•}

•})

•})

求一段HTML代码,使无论页面滚动到哪里,标题栏始终在屏幕顶部。。求此代码,谢谢

style type="text/css"

body{margin:0;}

.top{position:fixed; top:0; left:0; width:100%; height:20px; background:#FCC;}

.cn{height:1000px; background:#ccc; margin-top:20px;}

/style

div class="top"一直固定在顶部/div

div class="cn"模拟页面高度/div

满意请采纳

求助:如果使用js\jq 控制一个div 当滚动到页面顶部的时候固定在顶部,离开可继续滚动

1、新建html文档。

2、书写hmtl代码div class="box"    !--代码开始--div class="main"/divdiv class="sub"div class="sub01"/divdiv class="sub01"/divdiv class="fixed"我是固定的哟/div/div!--代码结束--/div。

3、书写css代码。style type="text/css"**{padding:0px;margin:0px;}.box{width:1000px;

;background:#ccc;margin:0 auto;overflow:hidden;}.main{width:770px;height:3000px;。

;background:#000;float:left;}.sub{width:220px;background:#FC6;float:right;}.。

4、sub01{width:220px;height:100px;background:#0CC;margin-bottom:10px;}.fixed

{width:220px;height:300px;background:#F66;font:normal 13px/30px \5FAE\8F6F\96C5\

9ED1;text-align:center;top:10px;}/style。

5、书写并添加js代码。script src="js/jquery.js"/script。这就完成了。

关于div当滚动到页面顶部的时候固定在顶部实例代码和js指定div滚动到顶部的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/12238.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载