Javascript滚动到顶部脚本修订

时间:2013-11-15 20:36:41

标签: javascript html css scrolltop

我正在尝试更改此脚本,以便当topcontrol div元素不可见(不透明度为0)时,它不可单击,因为此时即使topcontrol div的不透明度为0,仍然可以单击它(并且仍然可以调用移动到顶部的功能) - 如果你将鼠标悬停在div所在的位置,它会显示标题(虽然我打算删除标题,所以只需要确保点击它什么也没做)。此外,我似乎无法更改css游标属性,此时我尝试在设置visibility属性时将其与不透明度值一起更改,但它似乎不起作用,它只是保持最初设置的状态如

任何建议/帮助都会非常感激,因为我的javascript知识很漂亮。

//** jQuery Scroll to Top Control script- (c) Dynamic Drive DHTML code library:     http://www.dynamicdrive.com.
//** Available/ usage terms at http://www.dynamicdrive.com (March 30th, 09')
//** v1.1 (April 7th, 09'):
//** 1) Adds ability to scroll to an absolute position (from top of page) or specific element on the page instead.
//** 2) Fixes scroll animation not working in Opera. 


var scrolltotop={
//startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
//scrollto: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top).
setting: {startline:500, scrollto: 0, scrollduration:600, fadeduration:[600, 400]},
controlHTML: '<div class="scroll-button"></div>', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
controlattrs: {offsetx:20, offsety:100}, //offset of control relative to right/ top of window corner
anchorkeyword: '#top', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

state: {isvisible:false, shouldvisible:false},

scrollup:function(){
    if (!this.cssfixedsupport) //if control is positioned using JavaScript
        this.$control.css({cursor:'default', opacity:0}) //hide control immediately after clicking it
    var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto)
    if (typeof dest=="string" && jQuery('#'+dest).length==1) //check element set by string exists
        dest=jQuery('#'+dest).offset().top
    else
        dest=0
    this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
},

keepfixed:function(){
    var $window=jQuery(window)
    var controlx=$window.scrollLeft() + $window.width() - this.$control.width()  - this.controlattrs.offsetx
    var controly=$window.scrollTop() + $window.height() -  this.$control.height() - this.controlattrs.offsety
    this.$control.css({left:controlx+'px', top:controly+'px'})
},

togglecontrol:function(){
    var scrolltop=jQuery(window).scrollTop()
    if (!this.cssfixedsupport)
        this.keepfixed()
    this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
    if (this.state.shouldvisible && !this.state.isvisible){
        this.$control.stop().animate({cursor:'pointer', opacity:1}, this.setting.fadeduration[0])
        this.state.isvisible=true
    }
    else if (this.state.shouldvisible==false && this.state.isvisible){
        this.$control.stop().animate({cursor:'default', opacity:0}, this.setting.fadeduration[1])
        this.state.isvisible=false
    }
},

init:function(){
    jQuery(document).ready(function($){
        var mainobj=scrolltotop
        var iebrws=document.all
        mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
        mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body')
        mainobj.$control=$('<div   id="topcontrol">'+mainobj.controlHTML+'</div>')
            .css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', top:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'default'})
            .attr({title:'Scroll Up'})
            .click(function(){mainobj.scrollup(); return false})
            .appendTo('body')
        if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
            mainobj.$control.css({width:mainobj.$control.width()})    //IE6- seems to require an explicit width on a DIV containing text
        mainobj.togglecontrol()
        $('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
            mainobj.scrollup()
            return false
        })
        $(window).bind('scroll resize', function(e){
            mainobj.togglecontrol()
        })
    })
}

脚本可以在这里看到 - jakobfolin.com/portfolio.html     }

scrolltotop.init()

0 个答案:

没有答案