ورود

View Full Version : سوال: مشکل با جاوا اسکریپت برای داینامیک کردن منو



masouddousam
پنج شنبه 29 تیر 1396, 08:37 صبح
سلام
روز خوش!


مشکل: قالب تک برگ هست و با اسکرول کردن به سمت پایین و رسیدن به هر قسمت از صفحه منوی مربوط به همون قسمت مثلا تماس با ما highlight میشه یا رنگ زمینه اون منو تغییر میکنه.
من این قالب رو به وردپرس تبدیل کردم ولی برای قسمت نمیدونم چکار باید بکنم.


ممنون از راهنمایی تون:لبخندساده:

anvar
پنج شنبه 29 تیر 1396, 11:16 صبح
شما اگر در قالب html خودتون می تونید این کار رو انجام بدید پس با درج همین کدهای جاوااسکریپت در وردپرس هم می تونید اینکارو انجام بدید

برای درج کدهای جاوااسکریپت در صفحه می تونید در فایل header.php قالب خودتون دستور اضافه نمودن فایل جاوااسکریپت رو در قسمت head اضافه کنید تقریبا مشابه با صفحات html معمولی مثلا

<script type="text/javascript" src="/scripts/myscript.js"></script>

masouddousam
پنج شنبه 29 تیر 1396, 11:55 صبح
شما اگر در قالب html خودتون می تونید این کار رو انجام بدید.
<script type="text/javascript" src="/scripts/myscript.js"></script>

قالب رو خودم طراحی نکردم برای تمرین کردن از یه سایت خارجی دانلود کردم. ولی همه قسمت ها که به جاوا اسکریپت و جی کوئری مربوط میشه درست کار میکنه الا همین یه قسمت یه راهنما به زبان انگلیسی پیدا کردم که مشکل رو حل کرده بود ولی متاسفانه من تخصص زیادی در کد نویسی و جاوا و غیره ندارم و در حال تمرین هستم متوجه نشدم کدی که میگه کجا باید کپی کنم؟!

لینک راهنمای انگلیسی:
https://stackoverflow.com/questions/32395988/highlight-menu-item-when-scrolling-down-to-section



// cache the navigation links var $navigationLinks = $('#navigation > ul > li > a');
// cache (in reversed order) the sections
var $sections = $($(".section").get().reverse());

// map each section id to their corresponding navigation link
var sectionIdTonavigationLink ={};
$sections.each(function(){
var id = $(this).attr('id');
sectionIdTonavigationLink[id]= $('#navigation > ul > li > a[href=#'+ id +']');
});

// throttle function, enforces a minimum time interval
function throttle(fn, interval){
var lastCall, timeoutId;
returnfunction(){
var now =newDate().getTime();
if(lastCall && now <(lastCall + interval)){
// if we are inside the interval we wait
clearTimeout(timeoutId);
timeoutId = setTimeout(function(){
lastCall = now;
fn.call();
}, interval -(now - lastCall));
}else{
// otherwise, we directly call the function
lastCall = now;
fn.call();
}
};
}

function highlightNavigation(){
// get the current vertical position of the scroll bar
var scrollPosition = $(window).scrollTop();

// iterate the sections
$sections.each(function(){
var currentSection = $(this);
// get the position of the section
var sectionTop = currentSection.offset().top;

// if the user has scrolled over the top of the section
if(scrollPosition >= sectionTop){
// get the section id
var id = currentSection.attr('id');
// get the corresponding navigation link
var $navigationLink = sectionIdTonavigationLink[id];
// if the link is not active
if(!$navigationLink.hasClass('active')){
// remove .active class from all the links
$navigationLinks.removeClass('active');
// add .active class to the current link
$navigationLink.addClass('active');
}
// we have found our section, so we return false to exit the each loop
returnfalse;
}
});
}

$(window).scroll( throttle(highlightNavigation,100));

// if you don't want to throttle the function use this instead:// $(window).scroll( highlightNavigation );

#navigation {position: fixed;
}
#sections {
position: absolute;
left:150px;
}
.section {
height:200px;
margin:10px;
padding:10px;
border:1px dashed black;
}
#section5 {
height:1000px;
}
.active {
background: red;}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="navigation">
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
<li><a href="#section5">Section 5</a></li>
</ul>
</div>
<div id="sections">
<div id="section1" class="section">
I'm section 1
</div>
<div id="section2" class="section">
I'm section 2
</div>
<div id="section3" class="section">
I'm section 3
</div>
<div id="section4" class="section">
I'm section 4
</div>
<div id="section5" class="section">
I'm section 5
</div> </div>

ممنون:لبخندساده:

anvar
پنج شنبه 29 تیر 1396, 15:06 عصر
کد فوق برای صفحات html معمولی بصورت زیر میشه - شما 3 نوع کد دارید - کدهای html - CSS و JAVASCRIPT
یک پوشه بسازید با نام مثلا example - در داخل این پوشه 2 پوشه دیگه با نام های css و js بسازید -
اول یک فایل html بسازید و در پوشه example که در بالا ایجاد کردید ذخیره نمائید - کد زیر رو بصورت کامل در این فایل html ساخته شده Copy & paste کنید - این شد صفحه html شما
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>

<body>
<div id="navigation">
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
<li><a href="#section5">Section 5</a></li>
</ul>
</div>
<div id="sections">
<div id="section1" class="section">
I'm section 1
</div>
<div id="section2" class="section">
I'm section 2
</div>
<div id="section3" class="section">
I'm section 3
</div>
<div id="section4" class="section">
I'm section 4
</div>
<div id="section5" class="section">
I'm section 5
</div>
</div>
</body>
</html>

حالا یک فایل CSS با نام style بسازید و در پوشه css ساخته شده در بالا ذخیره کنید - کد زیر رو درون اون Paste کنید - این شد فایل CSS شما

#navigation {
position: fixed;
top: 0;
left: 250px;
}
#sections {
position: absolute;
top: 0;
left: 400px;
}
.section {
height: 200px;
margin: 10px;
padding: 10px;
border: 1px dashed black;
}
#section5 {
height: 1000px;
}
.active {
background: red;
}

حالا یک فایل جاوااسکریپت با نام script بسازید و با پسوند js در پوشه js ساخته شده در بالا ذخیره کنید و کد زیر رو مشابه با بالا Copy & paste کنید - این شد کدهای جاوااسکریپت شما

window.onload=function(){
$(document).ready(function() {
var $navigationLinks = $('#navigation > ul > li > a');
var $sections = $(".section");
var $sectionsReversed = $($(".section").get().reverse());
var sectionIdTonavigationLink = {};

$sections.each(function() {
sectionIdTonavigationLink[$(this).attr('id')] = $('#navigation > ul > li > a[href=#' + $(this).attr('id') + ']');
});
function setnavlinks() {
var scrollPosition = $(window).scrollTop();
$sectionsReversed.each(function() {
var currentSection = $(this);
var sectionTop = currentSection.offset().top;

if (scrollPosition >= sectionTop) {
var id = currentSection.attr('id');
var $navigationLink = sectionIdTonavigationLink[id];
if (!$navigationLink.hasClass('active')) {
$navigationLinks.removeClass('active');
$navigationLink.addClass('active');
}
return false;
}
});
}
$(window).scroll(function() {
setnavlinks();
});
});
}

برای اینکه کدهای بالا درست کار کنن شما نیاز به فایل Jquery دارید که لینک اون رو بصورت

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

دادم - لذا نیاز دارید که برای تست کدهای بالا به اینترنت متصل باشید در غیر اینصورت فایل جی کوئری رو دانلود کنید و به پوشه js منتقل نمائید و خط بالا رو برای خودتون اصلاح کنید. الان به رفتن به هر بخش زیر منوی مربوط active میشه و رنگش فرق می کنه

قالب وردپرس هم شبیه به همین فایل html معمولی میشه