<a href="#id"> without it going to where said #id is - html

http://jsfiddle.net/U6aKT/
go to id
<div style="margin-top:2000px;"></div>
<a id="id">id</a>
Based on the above example, is it possible for <div id="id"></div> to pop up without the page scrolling to where <div id="id"></div> is?

According to your question and comment under diffrent answer you want to scroll down to #id div or pop up the #id div when the link is clicked. If want to scroll then you have to use jquery animate function with scrollTop function; use the first way. And if you want pop up then use the second way.
First way:
Here is the JsFiddel with scroll animate
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
go to id
<div style="margin-top:2000px;">TEST</div>
<a id="id">id</a>
<script type="text/javascript">$(document) .ready(function() {
$('[href="#id"]').click(function() {
$("html,body") .animate({scrollTop: $("#id").offset().top},1200, "easeOutBounce");
return false;
});
});</script>
Second Way:
Here is JsFiddle
You have to use bootstrap modal. You just need to initialize the the anchor tag data-target attribute value as data-target="#id". And have to load the bootstrap css as well as jquery and bootstrap js. After that make your div class="modal fade" id="id". And also add the modal content, header, footer. Enjoy!
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" >
go to id
<div class="modal fade" id="id" tabindex="-1" role="dialog" aria-labelledby="id" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
Header
</div>
<div class="modal-body">
Body
<div class="modal-footer">
Footer
</div>
</div>
</div>
</div>
</div>
<script src = "https://code.jquery.com/jquery.js"></script>
<script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" > </script>

Your best option is probably using javascript.
Assuming jQuery is already initialized:
$('[href="#id"]').click(function(e){ // bind clicking said link
e.preventDefault(); // alternatively: e.stopPropagation();
});
You could also unbind and bind your click everytime, like so:
$('[href="#id"]').unbind('click').bind('click', function(e){ // bind clicking said link
e.preventDefault(); // alternatively: e.stopPropagation();
// EDIT:
// I **strongly** recommend you use a class such as `fakeLink`
// or something since targeting every `href` is a pain in the...
// so your selector would then be $('.fakeLink')
});
However, by default, it will look for an anchor by the name of #id, like so:
<a name="id"></a>
Hope this helps

Since the href="#id" mechanism is all about scrolling, what I can recommend is to attach a JavaScript method to all <A> elements on page and play a bit with the DOM when onClick.

Related

Creating some sort of SPA using Jquery

I'm trying to build an SPA using jquery. I have some html (nothing special).
I just have a menu and some divs. Depending on which menu item you click, a specific div should be displayed.
I can use .show() and .hide() and make use of id's and data-attributes.
But I was wondering if there are better ways to perform this kind of functionality.
I have simplified your code to the bone to show it easier
Since your ID on the triggers kind of matches your sub-menu, I have used them to target the content. You would put the code in doc ready.
$( "#" + this.id.replace("-tab", "-menu") ) this line simply uses the clicked ID and replaces the -tab with -menu. This is done to work with your current html
var $menu = $('#menu');
var $subMenu = $('.sub-menu');
$('.menu-item').on("click", function() {
$menu.hide();
$( "#" + this.id.replace("-tab", "-menu") ).show();
});
$('.back-btn').on( "click", function() {
// hide all sub-menu, no need to be specific
$subMenu.hide();
$menu.show();
});
.sub-menu {
display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid" id="menu">
<div id="weed-tab" class="menu-item">
<p class="menu-name">Wiet</p>
</div>
<div id="indica-tab" class="menu-item">
<p class="menu-name">Indica</p>
</div>
</div>
<div class="container-fluid sub-menu" id="weed-menu">weed
<div class="back-btn">
back
</div>
</div>
<div class="container-fluid sub-menu" id="indica-menu">indica
<div class="back-btn">
back
</div>
</div>

Open modal and scroll to a specific DIV

I have a link that opens a modal window.
I would like to open the modal window and scroll to a specific DIV inside it.
HTML:
read more
read more
read more
<div class="portfolio-modal modal fade" id="teamMembers" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-content">
<div class="container" data-id="teamMemebrScroll1">....</div>
<div class="container" data-id="teamMemebrScroll2">....</div>
<div class="container" data-id="teamMemebrScroll3">....</div>
</div>
JS:
// scroll to team member 2
jQuery(document).ready(function ($) {
$(".teamMemebrScroll2").click(function (event) {
event.preventDefault();
$('html,body').animate({ scrollTop: $(this.hash).offset().top }, 1000);
});
});
The modal opened correctly, but it always scrolling to the top.
In your approach, you are targeting the $('html,body') but you want to scroll to content inside modal when modal open so it should be $('modalselector')
You are trying to create multiple click events which you don't need and can achieve the same result by using BS modal event function by adding addtional data attributes in modal trigger button.
Made some changes in Modal trigger button, added data attribute data-id and no need the click event as you are trying.
read more
read more
read more
and BS modal event function
$('#teamMembers').on('shown.bs.modal', function (e) {
//alert('modal shown');
var id = $(e.relatedTarget).data('id'); // <--fetch modal button data-id when modal shown
});
As you already added data attributes data-id to elements inside modal
<div class="container" data-id="teamMemebrScroll1">....</div>
<div class="container" data-id="teamMemebrScroll2">....</div>
<div class="container" data-id="teamMemebrScroll3">....</div>
So just need to match the modal button data-id with element data-id inside the modal and scroll to it when modal open
$(document).ready(function () {
$('#teamMembers').on('shown.bs.modal', function (e) {
var id = $(e.relatedTarget).data('id'); // Modal button data-id
var team = $('.container[data-id="' + id + '"]'); // Element data-id with match the Modal button data-id
$(this).animate({ // Modal
scrollTop: team.offset().top // Scroll to the element
}, 1000);
});
});
Fiddle Working Example
If you want to have more control over scrolling using same above approach, way back I found a very small script and it does a little better job. Check the following
Fiddle with Scroll Top plugin
Try to when you open the window, the first thing you do is going to a link.
This link goes to the specific div "teamMembers"
<a href="#TeamMembers" ... />
Use Php on your window like this:
<?php
header('Location: #TeamMembers');
?>

Bootstrap v2.3.2 menu changes size after opening/closing it

The goal its to have a menu activated by a button.
This menu can have any type of content inside, so it must adapt according to its content.
In this example i have an accordion, but could be just a grid, or a form, since i'm making it as a bootstrap/jquery widget.
The problem is that the menu changes size after opening and closing it several times.
How can i improve it to make it adaptable to content and consistent, regarding that it will accept different contents.
Code
http://jsfiddle.net/fpmsusm5/
Javascript:
var button = $('#button');
var dialog = $('#modal');
button.on('click', function () {
dialog.toggleClass('hide');
dialog.position({
my: "right top",
at: "right-2 bottom",
of: button
});
})
$("#openpanel1").on("click", function () {
var curr_panel = $("#panel1")
curr_panel.toggleClass('show hide');
curr_panel.collapse(curr_panel.hasClass('show') ? 'show' : 'hide');
});
...
/* ensure any open panels are closed before showing selected */
$('#accordion').on('show.bs.collapse', function () {
$('#accordion .in').collapse('hide');
});
HTML:
<div class="pull-right">
<a id="button" class="btn">Settings</a>
</div>
<div id="modal" class="modal hide" style="overflow- y:hidden;width:auto;height:auto;max-height:100%; max-width:100% ">
<div class="modal-body" style="overflow-y:hidden; width:auto; height:auto; max-height:100%; max-width:100%">
<div id="accordion">
<button id="openpanel1" type="button" class="btn" style="width:100%;text-align:left"><i
class="icon-search"></i>Page Information
</button>
<div id="panel1" class="collapse">
<div class="panel-body">
Contents panel 1
</div>
</div>
....
</div>
</div>
</div>
Thanks for your insights.
Every time you position the dialog, it's changing the left property, and that's causing it to resize.
You don't have to reposition it every time you show/hide it, you can just do it once:
var dialog = $('#modal');
dialog.position({
my: "right top",
at: "right-2 bottom",
of: button
});
button.on('click', function () {
dialog.toggleClass('hide');
});
Then it stays the same size. Updated fiddle: http://jsfiddle.net/fpmsusm5/1/

DOM manipulation by angularjs direction

I read that Angularjs directives require a different approach than jquery. I am new to angularjs, so it will be great if somebody can explain how to use directives for this simple example. If you click on bottom div, then it moves (re-parent) the top image to the bottom div. I could add this jquery code on ng-click... but is there a better way?
JQUERY INTENT:
$("#bottom").click(function(){
$("#myimage").appendTo("#bottom");
});
ANGULARJS:
<div ng-app="myapp">
<div data-ng-controller="mycontroller">
<div id="top" style="background-color:red;width:200px;height:200px">
<img id="myimage" src="//placehold.it/150x150">
</div>
<div id="bottom" style="background-color:green;width:200px;height:200px">
</div>
</div>
</div>
Instead of listening for a click in jQuery, you can use Angular's ng-click directive to specify a function to call when the element is clicked and you can use the ng-if directive to add/remove the image, for example...
<div ng-click="appendImage()" id="bottom" style="background-color:green;width:200px;height:200px">
<img ng-if="showImage" id="myimage" src="//placehold.it/150x150">
</div>
Then in your controller...
angular.controller('myController', function ($scope) {
$scope.showImage = false;
$scope.appendImage = function (event) {
$scope.showImage = true;
};
});
A key difference between plain jQuery and Angular is that in jQuery you have to write code to manipulate the DOM yourself (like appending the image). If you use directives properly in Angular, you simply make changes to the $scope and directives will update the DOM for you automatically

mootools self reference

how can i handle a self-reference in mootools?
After i made the next container visible, i would remove the clicked "remove" Button.
With Jquery i can do it throug the "this" operator.
window.addEvent('domready', function(){
$$('div.showButton').addEvent('click', function(){
// show next Container
$$('div.container').getNext().show('inline');
// remove this "showButton"
$(this).remove() // not working
});
});
<!-- container 1 -->
<div class="container">
<div class="showButton">Show next container</div>
<div class="hideButton">hide this container</div>
</div>
<!-- container 2 -->
<div class="container" style="display:none">
<div class="showButton">Show next container</div>
<div class="hideButton">hide this container</div>
</div>
You have to remember mootools is not jquery and they different in the implementation of stuff.
First the $$ function return array of elements and not element so you can't call show on an array - you have to take the element you want (usually it's the first).
Second - you can call the current element using $(this) but it's a bit unnecessary because you are already inside the element event so you just can use "this":
http://jsfiddle.net/kk4gz/2/
$$('div.showButton').addEvent('click', function(){
// show next Container
$$('div.container')[0].getNext().show('inline');
// remove this "showButton"
this.remove();
});