i'm using the bxSlider plugin (a slider similar to jcarousel) on custom Magento store.
While the slider is istantiated on documet.ready() (in the head html section) by this way:
jQuery('#slider2').bxSlider({
pause: 4000,
auto: true,
autoControls: true,
displaySlideQty: 4,
moveSlideQty: 1
});
I want to change auto attribute to false in the php code if the slider items count is less than 5. I've tried to change it with:
<?php
// slider items count
$prom_count = $this->getPromotionalProducts()->count();;
?>
<?php if($prom_count<5): ?>
<script>
$("#slider2").attr('auto','false');
</script>
<?php endif; ?>
but it doesn't work, so i'm not sure that attribute of objects instantiated on dom ready can be changed by this way. anybody could help?
The attr-method from the jQuery-library works with attributes for HTML-elements:
<img src="..." alt="...">
In this case, src and alt are "attributes".
The bxSlider is configured by giving the bxSlider-function an array. You can't alter this array after it was passed to the function.
What you can do to toggle the auto-show is using bxSlider's stopShow()-function. To probably do this, you'll want to save the bxSlider-instance you created:
var slider = jQuery('#slider2').bxSlider({
...
});
So you can then call the function on this object:
slider.stopShow();
Doing this with PHP and JavaScript in a mixed way (like you suggested) is ugly and should be avoided.
Instead, you should use a JavaScript-only solution:
var slider = jQuery('#slider2').bxSlider({
auto: true,
...
});
if (slider.getSlideCount() < 5){
slider.stopShow();
}
Note that getSlideCount() returns the number of slides, which is not guarantied to be the number of Images in the gallery! The number of slides displayed at once can be set by the displaySlideQty-attribute. The default however is 1.
It seams that there is a Bug which causes the startShow and stopShow-functions not to work: https://github.com/wandoledzep/bxslider/pull/43
As a dirty workaround, you could do something like this:
<?php
echo "<script type=\"text/javascript\">
jQuery('#slider2').bxSlider({
pause: 4000,
autoControls: true,
displaySlideQty: 4,";
if($prom_count >= 5) echo "auto: true,";
echo "moveSlideQty: 1
});
</script>";
?>
Looks like there is no API function for this. Maybe the config will be loaded again if you use reloadShow(). If that does not help you have to read the source code.
That will set the auto attribute on the element it self, not the auto option of the plugin. It doesn't look like the plugin has the ability to change options after you have instantiated it, although I only had a quick look.
Can you not set the right auto value when you run the plugin by putting the php code first?
Related
I am using codesample plugin of tinymce which is mentioned here https://www.tinymce.com/docs/plugins/codesample/ in my custom portal.
Everything works fine, until, I have to "re-edit" the data stored in database.
When i go to edit, all data stored in database within
<pre><code><html> <p>Text</p> </html> </code></pre>
is stripped and shown as only
<pre><code>Text </code></pre>
when viewed from "Tools > Source"
I am already using "htmlentities" in my textarea like :-
<textarea><?php echo htmlentities($content); ?></textarea>
It still strips all html tag used inside the tag created by codesample plugin.
FAQ :
1. When adding data for the first time everything works fine.
2. Problem is only when i go to edit page. Tinymce strips only HTML code from codesample plugin. Rest all code that has been added using codesample like "css,python,php" etc., are displayed in editor.
The correct way to insert data into tinymce would not be to print it out or even using htmlentities. Consider the following code
<div class="editor" id="editor">
</div>
<script>
tinymce.init({
selector: 'div.editor',
theme: 'inlite',
plugins: 'image table link paste contextmenu textpattern autolink',
insert_toolbar: 'quickimage quicktable',
selection_toolbar: 'bold italic | quicklink h1 h2 h3 blockquote',
inline: true,
paste_data_images: true,
automatic_uploads: true,
init_instance_callback : function(ed) {
// ed.setContent("<h1>Title</h1><p>Content...</p>");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
ed.setContent(xhttp.responseText);
}
};
xhttp.open("GET", "content.php", true);
xhttp.send();
},
content_css: [
'//www.tinymce.com/css/codepen.min.css'
]
});
</script>
and the file content.php should just simply print the html content
<?php
$content = ''; // Fetch from database
print $content;
?>
notice the function at init_instance_callback while i initialize tinymce. That is the function called after tinymce is initialized. Now rather that directly using print inside the editor, make a ajax call inside init_instance_callback and render it there. I have put in a sample commented line just to help you out with the same. This will also take care of security validation (no script tag execution if any).
Also at the same time to get the contents of the editor while saving it to database is
var content = tinyMCE.get('editor').getContent();
And then you can make an ajax post request to save data to database.
Now why am i using ajax is important. I tried a lot of other methods where i could get away with directly printing it. But that causes a security flaw as script tags could run before the editor is initialized.
I used div in this example but it would be all the same even for text area. Also don't use htmlentities because that would escape the html content and you want to see the content rendered in tinymce and not the escaped version.
Ok, after lots of research, I found that it is an encoding issue. One need to encode each entity like :- < , > to < , > .
And & character inside <code> tag to & , that means < within <code></code> tag becomes <.
Thus, Code like below :-
<pre><code><html> <p>Text</p> </html> </code></pre>
should be like
<pre><code><html> <p>Text</p> </html> </code></pre>
So, for all users who are finding solution. This is the solution.
Remember, & inside <code> < </code> tag should only be converted to & . Notice < inside <code> tag is <
Cheers
Use setContent function to add the content once TinyMCE is loaded:
setup: function (editor) {
editor.on('init', function () {
var theContent = '<pre><code><html> <p>Text</p> </html> </code></pre>';
this.setContent(theContent);
});
}
Source: Tags Get Removed When Using Codesample Plugin with TinyMCE
Sorry to say but I find TinyMCE just too convoluted in dealing with "code". I put up with the numerous & n b s p ; that appear everywhere – but I've resorted to just adding images for code snippets or linking out to a modal popup.
I have a bxslider thats pretty standard, v4.0. I have the CSS creating the full screen effect in the background, and a large DIV on top. I want the DIV backgeround color to change with each slide, to color match the slide photo.
See Example
Is there a way to do this in the function?
Current function:
$(document).ready(function(){
$('.bxslider').bxSlider({
mode: 'horizontal',
auto:true,
pager:false,
nextSelector: '#bx-next',
prevSelector: '#bx-prev',
nextText: '<img src="btn_slider_next.png" />',
prevText: '<img src="btn_slider_prev.png" />',
});
});
BXSlider has some very extensive and well documented callback options that you can use when you instantiate the slider:
http://bxslider.com/options#onSliderLoad
You'll probably want onSlideAfter, so when you create the slider, it'll look like
$('.bxslider').bxSlider({
...
onSlideAfter: function (elem) { ... YOUR CODE HERE ... }
});
In order to change the color within that function, you could include the hex code you want associated with each image in an HTML5 data attribute.
<img src="img1.jpg" data-color="#ff0000" />
Then use the first argument passed to BX's onSlideAfter (which is the jQuery element for the current slide) to get that color and apply it to the background.
function (elem) {
$('#background-div').css('background-color', elem.data('color'));
}
so I have a jQuery accordion menu that I want to implement to my wordpress homepage, the problem is everytime I put the menu there, it extends my page way down like 1200 px height, and it`s the only one that does this.
How can I stop it from declaring that height and let me make it exactly how long the menu is extending, or even better go once with it. Again, I am talking about a wordpress plugin.
The theme I want to implement the menu in is : http://themeforest.net/item/celta-business-modern-corporate-wordpress-theme/full_screen_preview/218824?ref=lvraa
Left column
First you should try to set autoHeight to false , this will fix the height problem.
Then I could find the problem regarding HTML lists, I grabbed your HTML, put it locally and used the demo source for jQuery UI accordion (the section 3 have some HTML list inside) and it seems to work fine.
Hope it will Help :)
We all need your code to see the actual problem. You can jsfiddle it.
Also, try making the autoHeight to false.
$(".yourselector" ).accordion({ autoHeight: false });
When you initialize the accordion, include autoHeight: false in the options. It will then use the native heights specified in each div.
It is impossible to give you a sure answer without knowing which accordion plugin you are using. It would have been very helpful for you to give a URL that showcases your issue.
If you are using the jQuery UI Accordion, you can try:
initializing the accordion with autoHeight set to false.
explicitly set a height for the container of your accordion. for example:
< style>
#accordion {
height:200px !important;
}
< /style>
< div id="accordion">...< /div>
initialize the accordion with fillSpace set to true and then explicitly set the height of the accordion's parent element.
If you are using another library or if you developed the accordion functionality yourself, please tell us what library you are using and share your code with us.
You need to change autoHeight to false. Doing so may reduce the quality of animation, but should fix the height issue.
It is caused by the accordion taking the height of the largest div. Lots more about it here:
http://docs.jquery.com/UI/Accordion#option-autoHeight
$( ".selector" ).accordion({ autoHeight: false });
To overcome this issue, You must set two properties as below :-
$( ".selector" ).accordion({ autoHeight: false });
and
$( ".selector" ).accordion({ clearStyle: true });
Refer the - official jQuery accordion documentation
I need some help. I am bringing in a string from a custom field in wordpress. This string is an iframe embed code for both youtube and vimeo. I need to add a class to this iframe in order for fancybox to do its thing.
<?php
$videoLinks=get_post_meta($post->ID, "iframe video embed code", true);
echo $videoLinks;?>
<script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
When testing by inserting an iframe embed code in the custom field I get this. (no class added)
<iframe src="http://player.vimeo.com/video/33039612?title=0&byline=0&portrait=0" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p>HG Skis: Trillinton.. from HG Skis on Vimeo.</p> <script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
Relatively new to jquery, fancybox works on images manually tagged with fancybox class. Will adding class based on a certain event fix my issue?
try
<script>
$(document).ready(function(){
$('iframe').addClass('fancybox fancybox.iframe');
});
</script>
it should play the trick... of course, I assume you are using fancybox v2.x
It's possibly about timing. If fancybox is operating via a script that fires automatically on DOM ready, looking for "fancybox" elements to attach event handlers to, it might have already done its job by the time your addClass script fires. Something tells me this isn't necessarily the problem, though; the document ready function will actually be LATER than scripts running before the page is fully built.
There's also a syntax issue: 'fancybox fancybox.iframe' is not valid. You can add space separated classes if you want to add multiple, but "fancybox.iframe" is not a valid class. I believe JavaScript will let you add it, but you can't style it and there might be problems selecting it.
Don't you simply need $('iframe').addClass('fancybox')?
I'd make sure that your addClass is succeeding first, and if it is, are the classes required all present and accounted for. Then, debug timing issues.
The code you have shown works fine. That is, when I open a file with this it works:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<iframe>sfdfsf</iframe>
<script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
What does your fancybox code look like?
You can turn youtube links into embedded videos with something like this:
$(document).ready(function() {
$("a.youtube").click(function() {
$.fancybox({
'padding' : 0,
'autoScale' : false,
'transitionIn' : 'none',
'transitionOut' : 'none',
'title' : this.title,
'width' : 680,
'height' : 495,
'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
'type' : 'swf', // <--add a comma here
'swf' : {'allowfullscreen':'true','wmode':'opaque'}
});
return false;
});
Same thing can be done with Vimeo urls.
I'm building a dynamic navigation bar which is controlled by PHP, and I'm using images within my list. And I'm applying jQuery for the 'hover' effects. This is the PHP code:
$path = $_SERVER['PHP_SELF'];
$page = basename($path);
$page = basename($path, '.php');
And in my navigation list I'm setting 'display:none' and 'display:inline' depending on the return value of $page using a PHP 'if' statement. See code:
<ul id="cssdropdown">
<li class="headlink">
<a class="lightNav" href="index.php" <?php if($page == 'index'){echo "style='display:none !important'";}else{echo "style='display:inline'";}?>><img src="images/navButtons/home.png" /></a>
<a class="darkNav" href="index.php" <?php if($page == 'index'){echo "style='display:inline !important'";}else{echo "style='display:none'";}?>><img src="images/navButtons/home-dark.png" /></a>
</li>....
This is all working fine, the display of the Nav bar images change depending on what page the user is at. But my problem is now I'm trying to integrate jQuery to get a nice 'mouseover / hover' effect. See jQuery code:
$("li.headlink").hover(function(){
$(this).find("a.lightNav").css("display", "none");
$(this).find("a.darkNav").css("display", "inline");
},function(){
$(this).find("a.lightNav").css("display", "inline");
$(this).find("a.darkNav").css("display", "none");
});
But this is causing problems. When the user moves the cursor over the image in the Nav bar for the current page (ie the 'dark' image), it removes the display attribute set by PHP (obviously).
So I need a way to check on mouseover if 'darkNav' has display 'inline' or 'none' and tailor my jQuery from there, but for the life of me I cannot figure out how to do, I'm not the worlds greatest javascript/jQuery coder. Is there a way to check if an element has a particular CSS property applied to it, I googled and fiddled with my code for hours, but to no avail.
Thanks in advance everyone.
P.S. I added the CSS !important in my nav bar within the PHP if statement, but this for some strange reason, is only working in Chrome, all other browsers are ignoring this rule.
with jQuery you can check an attribute value like this:
... your code...
alert($(this).find("a.lightNav").css("display"))
As far as using jQuery to hide/show elements, simply use:
$(this).find("a.lightNav").hide()
$(this).find("a.darkNav").show()
No need to fiddle with "display", it's already built-into hide()/show(). You should not need use this either:
style='display:inline !important'"
By default the display is already block or inline, so unless you're using "display:none" you can usually lave these out.
If you have a left-floating menu you should be using float:left, not display:inline
Also inline styles override stylesheet CSS, so you should never need to use !important.
One hint at styling menus, style the A-tag, not the LI. Put the events on the A tags, not the LIs.
If you want to learn how to style menus properly, see my tutorial:
I love lists.
"So I need a way to check on mouseover if 'darkNav' has display 'inline' or 'none'.."
This is untested but you could try something like...
if ($(this).attr('style') == "display:none") {
// do something
}
else if ($(this).attr('style') == "display:inline") {
// do something
}
However, you may want to try addClass() and removeClass() instead of changing and/or using inline CSS.
yep..i'd second that using addClass()l and removeClass() would be loads better...some code like
if($(this).hasClass('visible') {
// do something
}
else {
// do something else
}
seems far better and professional and readable...but just me saying...