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

js固定在顶部代码(js顶部悬浮)

admin 发布:2022-12-19 18:43 135


本篇文章给大家谈谈js固定在顶部代码,以及js顶部悬浮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

导航固定在顶部的,要求导航随着页面往下走950像素之后更换logo,js怎么写?

window.onscroll = function() {

var topH = document.documentElement.scrollTop || document.body.scrollTop;//获取距离页面顶部的距离

var ele = document.getElementById("top");//你的导航栏id

if( topH = 80 ) {//当距离顶部超过XXX

             //换logo的代码

} else {

    //恢复第一个logo的代码

}

导航栏的css用定位悬浮即可

使用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"});

•}

•})

•})

提示出错:Uncaught TypeError: Cannot read property 'top' of undefined

$(function () {

var ie6 = document.all;

var dv = $('#article-index'), st;

dv.attr('otop', dv.offset().top); //存储原来的距离顶部的距离

$(window).scroll(function () {

st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);

if (st parseInt(dv.attr('otop'))) {

if (ie6) {//IE6不支持fixed属性,所以只能靠设置position为absolute和top实现此效果

dv.css({ position: 'absolute', top: st });

}

else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 });

}

else if (dv.css('position') != 'static') dv.css({ 'position': 'static',top:0 });

}

);

}

);

/script

是文章目录滚动到顶部固定的js代码,在有目录的地方会正常显示,但是如果一篇文章没有目录,则会提示如题错误,

请教了!

您的回答被采纳后将获得:

系统奖励20(财富值+经验值)+难题奖励30(财富值+经验值)

我要回答搜索相关资料

加粗

飘红

符号

编号

排版

图片

地图

代码语言

求助:如果使用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。这就完成了。

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固定在顶部的悬浮广告代码 带关闭按钮的 希望代码能够详细一点 谢谢

div style="width:100%;height:100px;position:fixed;"

button onclick="this.parentNode.parentNode.removeChild(this.parentNode)"关闭/button

/div

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

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

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


取消回复欢迎 发表评论:

分享到

温馨提示

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

联系我们反馈

立即下载