How make something like Facebook Notifications? - javascript

Please, read all the question before thinking wrong things.
I'm trying to implement a Facebook Notifications system like, but I need some help for style this. I already make the notifications, the back-end works perfectly, but I'm using a workaround for style - and truste me, this is slow, ugly and can't provide to me a good way to style the source.
What I'm exatcly talking about is the actions that occurs between "user click in Notifications" and "user receive loaded Notifications". I'm talking about the style of this part. Things like "how I can open a box with loaded notifications?" after click in a button/div/image/text.
Okay, let's continue with that.
Now I'm using Fancybox to create my notifications box - yes, FANCYBOX! Following is the visual that I get with Fancybox after click in "notifications" button (the "0"):
This may appears beautiful for you, but for me it's ugly - and this is really, really slow.
For make this workaround, I disabled the images of Fancybox, so when it loads, it loads without the black background, the "X" to close the window and other things. Following is my code.
Partially responsable for header:
<!-- This is the html generated by Ruby on Rails,number is dynamic-->
0
<script>
$(document).ready(function(){
$(".various").fancybox({
openEffect: 'none',
closeEffect: 'none',
prevEffect: 'none',
nextEffect: 'none',
fitToView : false,
autoSize: false,
scrolling: 'auto',
maxWidth:300,
maxHeight:500,
padding: 0,
leftRatio: 0.86,
topRatio: 0.18
});
});
</script>
This is generated HTML of notifications/index/ (for pratical example):
<div class="post group">
<div class="notifications-content">
<div class="group" id="notification-0">
<div class="post-middle">
<div class="notifications-title">
<b>Bruno postou:</b>
</div>
<div class="post-middle-text">
ADO
</div>
<div class="post-middle-actions with-dots">
<label> Postado às 12:34 de 17 de October</label>
</div>
</div>
</div>
</div>
<div class="notifications-content">
<div class="group" id="notification-1">
<div class="post-middle">
<div class="notifications-title">
<b>Fernando Paladini postou:</b>
</div>
<div class="post-middle-text">
hummmmmmmmm #boilo
</div>
<div class="post-middle-actions with-dots">
<label> Postado às 12:36 de 17 de October</label>
</div>
</div>
</div>
</div>
<div class="notifications-content">
<div class="group" id="notification-2">
<div class="post-middle">
<div class="notifications-title">
<b>#Ramon Diogo postou:</b>
</div>
<div class="post-middle-text">
http://www.youtube.com/watch?v=A3zPI-DBf7U
</div>
<div class="post-middle-actions with-dots">
<label></label>
<label>Comentado às 12:47 de 17 de October</label>
</div>
</div>
</div>
</div>
</div>
How I can do that with just HTML, CSS and jQuery / JS? I mean, this effects of when click on button open a "box" with the loaded contents, and when click out closes the "box". I really, really have no idea how to make this. Actually I'm using Fancybox opening a modal, but I don't want open a model, I want make this like Facebook and sites like Trello do - just css/jquery.
Note that I'm not asking for code, I need help because I don't have any ideia how to do this. I want the instructions: what I need make to when click the button, open the notifications box. Or when click outside notification box, close the notification box. The concepts behind this, but I will consider if you post some code too, because will help me with the learning.

You can do that with absolute positioning on the div that contains your notifications.
It should be hidden as a default behavior and you make it visible on click with javascript.
This will help you: http://net.tutsplus.com/tutorials/html-css-techniques/how-to-create-a-drop-down-nav-menu-with-html5-css3-and-jquery/ but you should change the behavior to react to the click event instead of mouseenter (line 14 of the script part). Replace that part with something like:
$(this).click(function(e) {
$(this).find("ul").stop(true, true).slideDown();
e.preventDefault();
});
notice the "preventDefault part? that is to avoid the browser to enter the link when you click.

Related

Open url/file when clicking bootstrap accordion

I need help with twitter's bootstrap accordion. I need it to where when i click the accordion title, the title opens up something i href it to.
<div class="container">
<p class="max-width-700 lead">testing</p>
<div class="col-md-6 col-sm-6 mb-sm-30">
<div class="accordion">
<div class="accordion-title">
2014 (Opening)
</div>
<div class="accordion-title">
2015
</div>
<div class="accordion-content">
</div>
Something like this, so when i click 2014, it opens up a document or file i have linked it to
You need to open file inside the accordion tab or need open file only like open pdf file an other window ?
In bootstrap you can't open link until you remove its according function
(1)
Use a boostrap-collapse.js event. See http://getbootstrap.com/2.3.2/javascript.html#collapse
Give each title a unique id and then add the following js to the end of your HTML.
$('#myItem1').on('show', function () {
window.location.href = '1.0.pdf';
})
(2)
The accordion is specifically designed to open an underlying hidden content. Using it to open external files is not expected behaviour - I would strongly recommend not using this type of UX - some users will see this as clickbait.
(3)
The .accordion class comes from Boostrap 2(?) 2 is no longer supported.

Fading Images with links

I have this code for the header
<div id="header">
<IMG SRC="http://danithemes.fanscity.eu/shugar/wp-content/uploads/2014/11/header-principal.png">
</div>
And this code for the main menu
<div id="menu">
Link One,
Link Two,
Link Three
</div>
I want the header image to fade into another image through the main menu links. Is this possible? Thanks.
First of all most W3C folks will get mad at you for this line <div id="header">
Anything syntactically named with an id the same as a generic HTML object tag needs to just be that tag. Anything good enough to give an id of id='header' should probably just be a <header> tag.
Secondly, I am unsure what the question is asking fully so let's go with something not yet said. #Parody showed a fiddled way of having the images change on click. The part of your question that said I want the header image to fade into another image through the main menu links. Is this possible? is difficult to understand so I am going to assume that you want some kind of event to trigger the changing of the images? There are many ways to do this but the best of which (especially for beginning programmers) is to use Bootstrap version 3.0+ since it comes with HTML driven stuff that usually requires JavaScript/JQuery to accomplish.
If you don't want to use Bootstrap then that's fine here is an example of how to use a hover event to trigger the change using JQuery...
HTML
<div id="header">
<img src="http://danithemes.fanscity.eu/shugar/wp-content/uploads/2014/11/header-principal.png" />
</div>
<div id="menu">
Link One
Link Two
Link Three
</div>
JAVASCRIPT/JQUERY
$(".navLink").each(function() {
$(this).hover(function() {
$("#header img").css({"background-image":"url($(this).attr('data-image'))"});
});
});

jQuery Mobile popup data-dismissible="false" problems

This is my first time using the jQUery Mobile popups. I've found documentation here and here. How I'm looking to create the following:
Now according to the doc's, the following code should work:
Open Popup
<div data-role="popup" id="popupBasic" data-dismissible="false">
<p>This is a completely basic popup, no options set.<p>
</div>
But given the code above I still keep getting the stock standard popup:
Any idea what I am doing wrong?
data-dismissible means whether you want the popup to close once clicked outside it. The default value is true, if you set it to false, you have to add a button with data-rel="back" to close it, jQM wont add a close button dynamically/automatically.
Change your markup to the following.
<div data-role="popup" id="popupBasic" data-dismissible="false" data-theme="c" data-overlay-theme="a">
<p>Click button to close this.</p>
Close
</div>
Note that data-theme and data-overlay-theme are different, the latter changes the color of the popup's overlay.
Or, you can close it programmatically.
$("#popupBasic").popup("close");
Demo

Stacked jQuery UI accordions not resizing

Be gentle I am new to this web dev game. I'm just trying to learn.
CODE:
<h4>Monday</h4>
<div id="first_accordian">
<h5>First Section</h5>
<div>
Some stuff.
</div>
<h5>Second Section</h5>
<div>
Some stuff.
</div>
</div>
<h4>Tuesday</h4>
<div id="second_accordian">
<h5>First Section</h5>
<div>
Some stuff.
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("[id$=_accordian]").accordion({ collapsible: true, active: false });
});
</script>
Obviously this is just a simplification of a partial view I have in and ASP.NET MVC3 app. But I am using jQuery UI and the accordion widget, so you will need to reference those for the code above to work.
QUESTION:
I have an issue here that when I expand an accordion element it will overlay any HTML underneath it. The behaviour I want is that I have a set of stacked accordions that all shift up or down when any element is selected in any of the accordions.
Does anyone know how I can do this with stacked accordions? Or thinking laterally. Is there any way I can add static headers that aren't accordion elements inline to the accordion (show above as the headers above each accordion; Monday, Tuesday)?
Is there are resizing event of something I should bind to? I'm at a loss as to where to start, any pointers welcome!
EDIT: Have just copied my own code into a file to check and experiment... and it works exactly how I need it to. But it doesn't in my app. So I'm guessing this must be down to something else going on outside of my code.
So... New question: What could stop something from resizing and creating this overlay problem?

Many different popups needed; Is it better to use one abstracted popup to handle every scenario

I have 3 different types of modal windows to handle different events. They popup to handle either:
1) Warnings, i.e. "hey, if you leave the page you will lose your work" kind of messages.
2) Dynamic data from a form the user previously filled out, i.e. "you are about to create a page with DataX: Data X, DataY: DataY, Date: March 28, 2012.
3) Form for the user to fill out.
I was originally trying to handle all 3 of the above scenarios with one set of html/css/javascript, just passing in data and modifying divs depending on situation. As I have expanded things, i'm noticing things being harder to maintain cleanly without being sloppy about it. I have a few questions which i'll list after I walk through how things are now.
Simplified HTML for modal popups:
<div id ="popup" style="display:none;">
<div class="modal-container">
<div class="modal">
<div class="modal-header">
<a class="close" href="# " title="Press Esc to close"><div id="close-label" title="Press Esc to close">Close</div></a>
<span id="top-header">WARNING</span>
</div>
<div class="modal-body">
<div class="modal-subhead">
<img class="subhead-icon" border="0" src="">
<span id= "header">WARNING</span>
</div>
<div class="modal-message"><span>If you leave this page, you will lose unsaved changed.</span></div>
<div class="modal-input-first">
<div class="modal-label"></div>
<div id="modal-input"></div>
</div>
<div class="modal-input-second">
<div class="modal-label"></div>
<div id="modal-input"></div>
</div>
<div class="modal-footer">
<a class="secondary" href="#">Cancel</a>
<span id = "button-secondary-label">or</span>
<span class="btn_container">
<span class="btn_left"></span>
<span class="btn_center">Continue</span>
<span class="btn_right"></span>
</span>
</div>
</div>
Right now this is located in a separate file with it's css and javascript and is loaded when the page is loaded.
In the main file I am changing the popup depending on what triggered the popup, just using jquery as such:
$('.modal-message').html('New Message');
$('.modal-subhead #header').text('New header');
The dynamic data is filled in similarly after first making the ajax request to get the data.
As I said, as things grow, i'm having to change a lot of this, and it's starting to feel sloppy. Especially as i'm having to make pixel perfect changes to the css for individual elements. Would it be best to have 3 completely separate sets of html to handle these? Where is the best place for this to live, is the separate file approach?
Is it better to keep adding separate elements as I need specific ones for certain instances, like so:
<div class="modal-input-specific-thing">
<div class="modal-label"></div>
<div id="modal-input"></div>
<div id="modal-input-2"></div>
</div>
Or to use jquery to change the structure of the html on the fly?
var new_input = 'modal-input' + count;
$('#modal-input').attr('id', new_input);
Is there a better method for pushing only the elements needed to a popup like this?
If I had 10-12 scenarios, which is possible as I keep expanding this. Is there a big enough performance hit to parsing 12 different sets of html/css every time page is loaded vs doing what i'm trying to do now...Making a generic/abstracted popup window, and push elements/data to it as the user needs?
Thanks!
I should do this with jQuery and jQuery UI. The jQuery UI has a real nice popup function called dialog. ( http://jqueryui.com/demos/dialog/ )
A working version that reconstruct your situation you will see here. ( http://jsfiddle.net/7GVmz/ )
There are a lot of options you can add so as buttons, callbacks on opening and closing. You can read all of it in the documentation on the jqueryui website ( http://jqueryui.com/demos/dialog/#option-disabled )
Html
<div id="popup-1" title="popup-1">
Hello World
</div>
<div id="popup-2" title="popup-2">
Hello World
</div>
<div id="popup-3" title="popup-3">
Hello World
</div>
JS
$(document).ready(function() {
$('#popup-1').dialog();
$('#popup-2').dialog();
$('#popup-3').dialog();
});​
Note
Its important that you load the jquery library and the jquery ui library.
​
I use SweetAlert for all my JavaScript alerts, it's a Better JavaScript Popup Box than the default and is really simple to use.
swal({<br>
title: "Are you sure you want to leave?",<br>
text: "You have unsaved changes!",<br>
type: "warning",<br>
showCancelButton: true,<br>
confirmButtonClass: "btn-danger",<br>
confirmButtonText: "Yes, Exit!",<br>
closeOnConfirm: false<br>
},<br>
function(){<br>
swal("Deleted!", "Your imaginary file has been deleted.", "success");<br>
});<br>
A full tutorial is here - https://kodesmart.com/kode/javascript-replacement-alert-box

Categories

Resources