Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
Create your own right-click contextmenu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Finley" data-source="post: 289468" data-attributes="member: 7334"><p>Hi there,</p><p></p><p>I am going to explain to you how to make your custom right-click contextmenu. I am going to explain every part.</p><p>It is very simple. We will be using jQuery so include the library in your <head> tag.</p><p></p><p>I am asuming you have the [very, very] basic knowledge of HTML, CSS and JavaScript</p><p><strong>Step 1</strong></p><p>We begin with creating an simple eventlistener.</p><p>[code]</p><p>window.addEventListener('contextmenu', function(e) {</p><p> // When you click on the contextmenu, trigger this function</p><p>})</p><p>[/code]</p><p></p><p><strong>Step 2</strong></p><p>In this step we will be creating the 'pop-up' of our own context menu. We are using the jQuery .css() function for that.</p><p>[code]</p><p> $('.rightclickmenu').css({ // $('CHANGE_THIS_TO_YOUR_MENU_CLASS')</p><p> "margin-left": e.clientX, // position of the mouse X</p><p> "margin-top": e.clientY // position of the mouse Y</p><p> }).show() // Showing the actual menu</p><p>[/code]</p><p></p><p><strong>Step 3</strong></p><p>In the 3rd step we are going to disable the default context menu, so it doesn't show up when you right-click your screen.</p><p>For this, is one simple line needed.</p><p>[code]</p><p>e.preventDefault();</p><p>[/code]</p><p></p><p><strong>Step 4</strong></p><p>In the 4th and final step we are going to make the custom menu dissapear, as soon as you click somewere out of the menu.</p><p>We will be using another eventlistener for that</p><p>[code]</p><p>window.addEventListener('click', function(){ // as soon as you click</p><p> $('.rightclickmenu').hide(); // hide the contextmenu</p><p>})</p><p>[/code]</p><p></p><p><strong>Okay then, lets put it all together now</strong></p><p></p><p>It will look something like this:</p><p>[code]</p><p>window.addEventListener('contextmenu', function(e) {</p><p> $('.rightclickmenu').css({</p><p> "margin-left": e.clientX,</p><p> "margin-top": e.clientY</p><p> }).show()</p><p></p><p> e.preventDefault();</p><p> window.addEventListener('click', function(){</p><p> $('.rightclickmenu').hide();</p><p> })</p><p>}) </p><p></p><p></p><p>[/code]</p><p></p><p>For a full HTML page: <a href="http://pastebin.com/SKtFiEgL" target="_blank">http://pastebin.com/SKtFiEgL</a></p><p>For a live demo: <a href="http://finleyhd.nl" target="_blank">http://finleyhd.nl</a></p><p></p><p>Kind regards</p><p></p><p>Finley</p></blockquote><p></p>
[QUOTE="Finley, post: 289468, member: 7334"] Hi there, I am going to explain to you how to make your custom right-click contextmenu. I am going to explain every part. It is very simple. We will be using jQuery so include the library in your <head> tag. I am asuming you have the [very, very] basic knowledge of HTML, CSS and JavaScript [b]Step 1[/b] We begin with creating an simple eventlistener. [code] window.addEventListener('contextmenu', function(e) { // When you click on the contextmenu, trigger this function }) [/code] [b]Step 2[/b] In this step we will be creating the 'pop-up' of our own context menu. We are using the jQuery .css() function for that. [code] $('.rightclickmenu').css({ // $('CHANGE_THIS_TO_YOUR_MENU_CLASS') "margin-left": e.clientX, // position of the mouse X "margin-top": e.clientY // position of the mouse Y }).show() // Showing the actual menu [/code] [b]Step 3[/b] In the 3rd step we are going to disable the default context menu, so it doesn't show up when you right-click your screen. For this, is one simple line needed. [code] e.preventDefault(); [/code] [b]Step 4[/b] In the 4th and final step we are going to make the custom menu dissapear, as soon as you click somewere out of the menu. We will be using another eventlistener for that [code] window.addEventListener('click', function(){ // as soon as you click $('.rightclickmenu').hide(); // hide the contextmenu }) [/code] [b]Okay then, lets put it all together now[/b] It will look something like this: [code] window.addEventListener('contextmenu', function(e) { $('.rightclickmenu').css({ "margin-left": e.clientX, "margin-top": e.clientY }).show() e.preventDefault(); window.addEventListener('click', function(){ $('.rightclickmenu').hide(); }) }) [/code] For a full HTML page: [URL]http://pastebin.com/SKtFiEgL[/URL] For a live demo: [URL]http://finleyhd.nl[/URL] Kind regards Finley [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
Create your own right-click contextmenu
Top