Javascript 移动到[取消]隐藏元素
我有一些jQuery,我正试图以一种特定的方式工作。我想隐藏和取消隐藏一个元素,并将焦点放在暴露的区域,一旦它被发现。有一个链接#welcomeselect,单击该链接时应显示隐藏元素#welcome。如果我点击带有以下代码的链接,它将仅取消隐藏元素;如果我再次单击它,它将移动到显示的元素。我在另一个网站(scrollToAnchor)上找到的自定义显示代码;我只需要能够移动到unhide元素,平滑过渡不是一个要求。我做错了什么Javascript 移动到[取消]隐藏元素,javascript,jquery,Javascript,Jquery,我有一些jQuery,我正试图以一种特定的方式工作。我想隐藏和取消隐藏一个元素,并将焦点放在暴露的区域,一旦它被发现。有一个链接#welcomeselect,单击该链接时应显示隐藏元素#welcome。如果我点击带有以下代码的链接,它将仅取消隐藏元素;如果我再次单击它,它将移动到显示的元素。我在另一个网站(scrollToAnchor)上找到的自定义显示代码;我只需要能够移动到unhide元素,平滑过渡不是一个要求。我做错了什么 $('#welcomeselect').click(functio
$('#welcomeselect').click(function(){
$('#welcome').show();
scrollToAnchor("#welcome");
});
// scroll handler http://bradsknutson.com/blog/smooth-scrolling-to-anchor-with-jquery/
var scrollToAnchor = function( id ) {
// grab the element to scroll to based on the name
var elem = $("a[name='"+ id +"']");
// if that didn't work, look for an element with our ID
if ( typeof( elem.offset() ) === "undefined" ) {
elem = $("#"+id);
}
// if the destination element exists
if ( typeof( elem.offset() ) !== "undefined" ) {
// do the scroll
$('html, body').animate({
scrollTop: elem.offset().top
}, 1000 );
}
};
// bind to click event - http://bradsknutson.com/blog/smooth-scrolling-to-anchor-with-jquery/
$("a").click(function( event ) {
// only do this if it's an anchor link
if ( $(this).attr("href").match("#") ) {
// cancel default event propagation
event.preventDefault();
// scroll to the location
var href = $(this).attr('href').replace('#', '')
scrollToAnchor( href );
}
});
已更新 此项在显示元素时移动(id中只有一个额外的哈希):
以前的 在这里,这一个只有在元素显示后才会移动:
$('#welcomeselect').click(function(){
$('#welcome').show(function(){
scrollToAnchor("welcome");
});
});
var scrollToAnchor = function( id ) {
// grab the element to scroll to based on the name
var elem = $("a[name='"+ id +"']");
// if that didn't work, look for an element with our ID
if ( typeof( elem.offset() ) === "undefined" ) {
elem = $("#"+id);
}
// if the destination element exists
if ( typeof( elem.offset() ) !== "undefined" ) {
// do the scroll
$('html, body').animate({
scrollTop: elem.offset().top
}, 1000 );
}
};
请编辑您的问题并删除所有空行!好了,很抱歉。
$('#welcomeselect').click(function(){
$('#welcome').show(function(){
scrollToAnchor("welcome");
});
});
var scrollToAnchor = function( id ) {
// grab the element to scroll to based on the name
var elem = $("a[name='"+ id +"']");
// if that didn't work, look for an element with our ID
if ( typeof( elem.offset() ) === "undefined" ) {
elem = $("#"+id);
}
// if the destination element exists
if ( typeof( elem.offset() ) !== "undefined" ) {
// do the scroll
$('html, body').animate({
scrollTop: elem.offset().top
}, 1000 );
}
};